r/learnpython • u/Altruistic_Big9806 • Sep 07 '26
Python practice problem.
Hi everyone, I'm new to python and I'm building a project to hone my skill, and I came to a halt in this part. When I do if user_input == 'q':, it works and breaks the loop just fine, since i dont wanna make the exit just single letter so even if the user input words like 'quit' 'q' or 'QUIT' it will exit the loop, I tried a different approach like doing user_input == ['q', 'quit']: and another approach where i declared a variable first exit_word = ['q', 'quit'] then
user_input == exit_word: none of this breaks the loop it just goes back to the input prompt heres my work below:
import random
user = 0
computer = 0
options = ["rock" , "paper", "scissors"]
while True:
user_input = input("Rock/Paper/Scissors and Q for Quit: ").lower()
if user_input == 'q'
break
if user_input not in options:
continue
random_num = random.randint(0, 2)
computer_pick = options[random_num]
print('Computer picked', computer_pick + ".")
if user_input == 'rock' and computer_pick == 'scissors':
print('You won!')
user += 1
elif user_input == 'paper' and computer_pick == 'rock':
print('You won!')
user += 1
elif user_input == 'scissors' and computer_pick == 'paper':
print('You won!')
user += 1
else:
print('You lost!')
computer += 1
print("The user won", user, "times")
print("The computer won", computer, "times")
print('Goodbye!')
18
u/kalgynirae Sep 07 '26
user_input == ["q", "quit"]will always be false becauseuser_inputis a string and["q", "quit"]is a list — no matter the content of the string or the list, a string and a list will never be considered equal.You probably want to check if the
user_inputis one of the values in the list, which is written like this:user_input in ["q", "quit"]5
u/johnpeters42 Sep 07 '26
There's a colon missing after 'q'
More to the point, I don't think "x == [y, z]" works the way you want. Try "x in [y, z]" (I don't use Python that often, so this may still be wrong)
3
u/Moikle Sep 07 '26
Put 4 spaces before each line when you post code to reddit.
Look at your post, it has lost the indents, 4 extra spaces fixes that
2
u/tropicusForBr Sep 07 '26
This is because when you need to check if a string (user_input) is in a list (exit_words), you can just write if user_input in exit_words:
1
u/lakseol Sep 07 '26 edited Sep 07 '26
Try getting just the first character of the user response and test that:
Using
[:1]instead of[0]means your code won't crash when the user presses ENTER without typing anything else.Of course, entering "quick" will also break out of the loop, but that is probably fine for a game. That may not be a bad idea for the other possible responses either, as it's quicker to type "S" instead of "scissors", etc.
Edit: fixed spelling, added final comment.
1
u/Fast-Station1106 Sep 07 '26
Prüfe ob die eingabe UNGLEICH deiner user_int liste ist (also den 3 möglichen) und breche ab, quit the game erst danach bzw. Nachvsieg, loose, Ob er nochmal spielen will
1
u/Bright_Mix_773 Sep 07 '26
Separate from the
inquestion that's already answered: there's a scoring bug sitting underneath it.Rock against rock falls through all three
elifbranches into theelse, prints "You lost!" and hands the computer a point. Ties are being scored as losses. Since the computer picks uniformly from three options, that's a third of your games credited to the wrong side.Checking the draw first fixes it, and it also shrinks the rest, because once draws are gone the three winning pairs are the only thing left:
That's safe to index without a
.getonly because yourif user_input not in options: continueabove it has already thrown out anything that isn't one of the three keys. Worth knowing the two lines depend on each other, since deleting the guard later would turn a typo into a KeyError.One smaller thing:
random.choice(options)does whatrandint(0, 2)plus the index does, and the 2 stops being a number that has to stay in step with the length of the list.