0

Problem with "while" & "break" in rock, paper, scissor program

Hi everybody - I'm trying to write a simple code where user will be allowed to make a string input. If they input rock, paper, or scissor, the program will move to the next step of having the computer choose rock, paper, or scissor. I made two lists: "win" & "lose", comparing what the user chose vs what the computer chose. Then depending of what the user chose & what the computer chose, the print-out should be "You win!" "You lose!" Or "It's a tie!" The actual output is always "It's a tie", no matter if user vs computer choice was in the win list, in the lose list, or in neither list. I'm also not sure of how to use the break function. NOTE: I used "while" instead of "if" in because I'd like to eventually carry this over so that the program runs three times instead of just once, each time comparing user input to the computer's random choice. Also, if there is any way to streamline my code, I'd love to hear it. Anyway, here's my code: ------ #play rock paper scissor against the computer #Choose rock, paper, scissor import random user = input() user = user.lower() print("User chooses: " + user) rps = ["rock", "paper", "scissor"] if user == "rock": user = rps[0] elif user == "paper": user = rps[1] elif user == "scissor": user = rps[2] elif user != rps[0] or user != rps[1] or user != rps[2]: print("You must input rock, paper, or scissor. All other entries are invalid.") while user == rps[0] or user == rps[1] or user == rps[2]: computer = random.choice(rps) print("Computer chooses: " +computer) win = [(rps[0] and computer[2]) or (rps[1] and computer[0]) or (rps[2] and computer[1])] lose = [(rps[0] and computer[1]) or (rps[1] and computer[2]) or (rps[2] and computer[0])] if win is True: print("You win!") elif lose is True: print("You lose!") else: print("It's a tie!") break

1st Jun 2017, 5:30 AM
Nasheed Shams
Nasheed Shams - avatar
5 ответов
+ 3
The problem is with your win-lose conditions: you refer to elements of rps as if it were user input, which it is not you try to choose second and third elements of a "computer" which are non-existent as it is a string, not a list Try this code if user == computer: print("It's a tie") elif (user == rps[0] and computer == rps[2]) or (user == rps[1] and computer == rps[0]) or (user == rps[2] and computer == rps[1]): print("You win") else: print("You lose") BTW, try next time posting not the code, but rather link to a playground with that code. Makes it much easier to help with
1st Jun 2017, 6:16 AM
Lana
Lana - avatar
+ 1
your win condition and lose can never evaluate to True thus the else statement will always run so the result would always be a tie even if the computer's guess result and the user's input is different , try printing your win , lose and see what it results to ? it never result to the Boolean True so your conditions to check win and loose Value to be True will never result to that that's why the else statement keeps on printing "its a tie" so lets work around this the truth is you are doing something close to a set imagine if the user entered "rock" and the computer random choice was "paper" your "win = [(rps[0] and computer[2]) or (rps[1] and computer[0]) or (rps[2] and computer[1])]" line of code would check first for rps[0] whose value is "rock" and computer[2] whose value is "paper"[2]--> p so then you have " and " work like a set intersection since p is not in "rock", the program moves to (rps[1] and computer[0]) and finds rps[1] == "paper" and computer[0] to be "p" at that point your win = ["p"] cause "paper" and "p" == "P" and in other ways the program might be checking for this things on an ascii level in conclusion the issue with your code is how you have assigned values to win, and lose these values are never evaluated to True so win can never be equal to True neither would Lose be
1st Jun 2017, 12:21 PM
John Emma
John Emma - avatar
+ 1
shams i actually agree with lana you should replace your code from line win = [(rps[0] and computer[2]) or (rps[1] and computer[0]) or (rps[2] and computer[1])] lose = [(rps[0] and computer[1]) or (rps[1] and computer[2]) or (rps[2] and computer[0])] if win is True: print("You win!") elif lose is True: print("You lose!") else: print("It's a tie!") to : if user == computer: print("It's a tie'") elif (user == rps[0] and computer == rps[2]) or (user == rps[1] and computer == [0]) or (user == rps[2] and computer == rps[1]): print("You win") else: print("You lose") before the break that should fix the bug you could also try it these way i have re-written your code in the code playground this is the URL https://code.sololearn.com/csryW0bCi03t/#py hopefully it works the only difference is i have added a function and replace some part of your code i hope this helps you
1st Jun 2017, 2:39 PM
John Emma
John Emma - avatar
+ 1
Hey John - took a look at your code online. Had a question: you had: Def Checker(user,com): print(user,com) if user == computer: return(tie) Computer is defined later on as a variable with computer = random.choice(rps) But "com" isn't explicitly defined, but the program still prints (user,com). Can you tell me how? Thanks!
1st Jun 2017, 7:12 PM
Nasheed Shams
Nasheed Shams - avatar
0
@shams i want to say thank you first for your observation, i already fixed it even though it worked and i escaped the bug it was still a wrong act cause i had defined parameters and i was supposed to do my checking with those arguments. it was meant to be this: Def Checker(user,com): print(user,com) if user == com: return(tie) but yes i escaped the error cause the variable computer was first defined "GLOBALLY" before i called the function "Checker" so it was easy to check for user equality between user and computer , try placing the line where "computer = random.choice(rps)", below the line "check = Checker(user, computer)" then you would get an error if the variable computer was defined and initialised in another function or class then we could have gotten an error cause that way computer is Gobal anymore it can only be accessed within the call or with the class
1st Jun 2017, 8:28 PM
John Emma
John Emma - avatar