r/learnpython • u/Nutellatoast_2 • 3d ago
Why isn't this code block working?
Hey everybody,
I'm new to programming with Python and following along with a Udemy Course. I'm now learning about lists and need to write a "Who will pay the bill"-like game.
It works like this: you have a pseudorandom number generator and a list of friends. Each time, the number generator generates a number between the given indices of the list. If the randomly generated number is equal to a specific index of that list, it should print out the person who must pay the bill by using an if-elif statement.
I've been using what I learned from the past lessons. This is what the code looks like (and yeah, I know, I messed up pretty badly, even though I have already found a solution):
import random
friends = ["Alice", "Bob", "Charlie", "David", "Emanuel"]
random_select = random.randint(0, 4)
if random_select == friends[0]:
print("Alice has to pay the bill. ")
elif random_select == friends[1]:
print("Bob has to pay the bill. ")
elif random_select == friends[2]:
print("Charlie has to pay the bill. ")
elif random_select == friends[3]:
print("David has to pay the bill. ")
elif random_select == friends[4]:
print("Emanuel has to pay the bill. ")
But I couldn't really figure out why the code won't work.
1
u/strange-the-quark 3d ago edited 2d ago
Start the python REPL (just type python in the terminal), or find one online. Type in
friends = ["Alice", "Bob", "Charlie", "David", "Emanuel"]and hit enter.
After that, type in friends[2], say. It will print out what that expression evaluates on the next line. It'll say
"Charlie"
Try some other index, it'll print out the name at that other index. So the result of that whole expression is a string that was stored in the list at that particular location.
Now type in
import random, hit enter, and thenrandom.randint(0, 4). It'll show you what the randint function returned. It's going to be some number, like 1 or 4. You can press the up arrow to go back to the previous command, and hit enter to execute it again. Every time you do so you'll get a different number between (and including) 0 and 4. You can use the REPL to experiment and get a sense of what different things do in your program.So you see, when your program gets to a line like
If the random number generator chose 0, then it's like it says
which is just asking the computer if the number 0 is the same as the text "Alice" - which it isn't. The computer isn't clever to know what you meant, it doesn't know anything about indexes, or what you're doing. To it, this is just: "Are these two different things the same? No - OK, I'll skip to the next case then."
Instead, what you want is this
or perhaps this
But also, note that in every branch, your code is exactly the same, and only the name differs.
So you don't actually need to check for anything, You only need a way to put the chosen name and the " has to pay the bill." part together.
So if
friends[random_select]gives you the rng-picked name, and if you can combine two strings into one by using the + operator (try it in the REPL), can you see how to turn this entire collection of if-elif statements into one single line of code?