Aug-24-2024, 07:43 PM
Hi , Thanks for viewing, hope all is good.
I'm trying to put together a rock paper scissors code and what i was trying to do as an improvement, was to to the user from inputting a non good input and only allowing the 3 inputs to play.
On first wrong input it does ask to input again but if i put in another wrong answer it takes the wrong answer and uses it, could someone please have a look and tell me what i'm doing wrong, or if there is a preferred robust method that programmers use that would be greatly appreciated.
i also seem to have an issue with trailing white space for some reason.
I'm trying to put together a rock paper scissors code and what i was trying to do as an improvement, was to to the user from inputting a non good input and only allowing the 3 inputs to play.
On first wrong input it does ask to input again but if i put in another wrong answer it takes the wrong answer and uses it, could someone please have a look and tell me what i'm doing wrong, or if there is a preferred robust method that programmers use that would be greatly appreciated.
i also seem to have an issue with trailing white space for some reason.
import random while True: mylist = ["rock","paper","scissors"] user_choice = input("Enter your choice(rock,paper or scissors):") while user_choice not in mylist: try: user_choice = input("Enter your choice(rock,paper or scissors):") except ValueError: continue else: break computer_choice = random.choice(["rock","paper","scissors"]) print("You chose: ",user_choice) print("Computer chose:",computer_choice) if user_choice == computer_choice: print("it's a tie!") elif(user_choice == "rock" and computer_choice == "scissors") or \ (user_choice == "paper" and computer_choice == "rock") or \ (user_choice == "scissors" and computer_choice == "paper"): print("you win!") else: print("Computer wins!") play_again = input("Do you want to play again?(yes/no):") if play_again != 'yes': break