![]() |
HELP---problems with rock paper scissors games - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: HELP---problems with rock paper scissors games (/thread-5042.html) |
HELP---problems with rock paper scissors games - kalt91 - Sep-15-2017 Im having problems with the while loop terminating the rock paper scissors game once it gets to the choice variable to say no. Also when i initially enter my input in the beginning, it will only give me 'tie' back in the shell. It wont give any other response. Its like the random choice isnt working. BTW im running in python shell 3.5.4. ----- import random ROCK=1 PAPER=2 SCISSORS=3 keeprunning = True while keeprunning == True: user = input("rock/paper/scissors: ") roll = random.randint(1,3) if user == 'rock' or user == 'Rock' and 'roll' == 'ROCK': print('tie') elif user == 'rock' or user == 'Rock' and 'roll' == 'PAPER': print('you lose') elif user == 'rock' or user == 'Rock' and 'roll' == 'SCISSORS': print('you win') elif user == 'paper' or user == 'Paper' and 'roll' == 'ROCK': print('you win') elif user == 'paper' or user == 'Paper' and 'roll' == 'PAPER': print('tie') elif user == 'paper' or user == 'Paper' and 'roll' == 'SCISSORS': print('you lose') elif user == 'scissors' or user == 'Scissors' and 'roll' == 'SCISSORS': print('tie') elif user == 'scissors' or user == 'Scissors' and 'roll' == 'ROCK': print('you lose') elif user == 'scissors' or user == 'Scissors' and 'roll' == 'PAPER': print('you win') choice = input('Do you want to play another game?: ') if choice == 'yes' or 'Yes': keeprunning == True else: keeprunning == False print('Good game') import random ROCK=1 PAPER=2 SCISSORS=3 keeprunning = True while keeprunning == True: user = input("rock/paper/scissors: ") roll = random.randint(1,3) if user == 'rock' or user == 'Rock' and 'roll' == 'ROCK': print('tie') elif user == 'rock' or user == 'Rock' and 'roll' == 'PAPER': print('you lose') elif user == 'rock' or user == 'Rock' and 'roll' == 'SCISSORS': print('you win') elif user == 'paper' or user == 'Paper' and 'roll' == 'ROCK': print('you win') elif user == 'paper' or user == 'Paper' and 'roll' == 'PAPER': print('tie') elif user == 'paper' or user == 'Paper' and 'roll' == 'SCISSORS': print('you lose') elif user == 'scissors' or user == 'Scissors' and 'roll' == 'SCISSORS': print('tie') elif user == 'scissors' or user == 'Scissors' and 'roll' == 'ROCK': print('you lose') elif user == 'scissors' or user == 'Scissors' and 'roll' == 'PAPER': print('you win') choice = input('Do you want to play another game?: ') if choice == 'yes' or 'Yes': keeprunning == True else: keeprunning == False print('Good game') RE: HELP---problems with rock paper scissors games - nilamo - Sep-15-2017 Ok, there's a lot to say about this, so I'll break it into parts. But first, next time you post code, please put it in code tags so the indentation is preserved. (Sep-15-2017, 04:25 PM)kalt91 Wrote: Its like the random choice isnt working. random.choice() works fine, but you never call it. Instead, you're getting a number (either 1, 2, or 3).Quote:if user == 'rock' or user == 'Rock' and 'roll' == 'ROCK': # ^^^^^^ You never compare the value of the variable roll anyway, so it doesn't matter what random.randint() returns. Instead, you compare the string "roll" with a couple of different things, which would always be False.Quote:elif user == 'rock' or user == 'Rock' and 'roll' == 'PAPER': # ^^^^ AND has a higher precedence than OR, so that line is exactly the same as this one: elif (user == 'rock') or (user == 'Rock' and "roll" == "PAPER"):...so if you type "rock", you'll tie every single time, regardless of what the opponent does. But only "rock" all lowercase. If you type "Rock" or "ROCK" or "rOcK", then you might not tie anymore. RE: HELP---problems with rock paper scissors games - micseydel - Sep-15-2017 Take a look at https://python-forum.io/Thread-Multiple-expressions-with-or-keyword |