import random
user = input("your move (R, S or P): ")
computer_move_as_int = random.randint(0, 2)
if computer_move_as_int == 0:
computer = "R"
elif computer_move_as_int == 1:
computer = "S"
else:
computer = "P"
print("I play", computer)
if user == computer:
print("it's a tie")
elif (user == "R" and computer == "S") or (user == "S" and computer == "P") or (user == "P" and computer =="R"):
print("you win")
else:
print("I win")
import random
while True:
user = input("your move (R, S or P): ")
if user == "R" or user == "S" or user == "P":
break
print("your input is not valid, try again !")
print()
computer_move_as_int = random.randint(0, 2)
if computer_move_as_int == 0:
computer = "R"
elif computer_move_as_int == 1:
computer = "S"
else:
computer = "P"
print("I play", computer)
if user == computer:
print("it's a tie")
elif (user == "R" and computer == "S") or (user == "S" and computer == "P") or (user == "P" and computer =="R"):
print("you win")
else:
print("I win")
Comment: The paraenthesis after elif are not needed, as and is evaluated before or. But as this can be forgotten easily or others who read the code are not aware of this I recommend to use them.
import random
while True:
while True:
user = input("your move (R, S or P, X to stop playing): ")
if user == "R" or user == "S" or user == "P" or user == "X":
break
print("your input is not valid, try again !")
print()
if user == "X":
break
computer_move_as_int = random.randint(0, 2)
if computer_move_as_int == 0:
computer = "R"
elif computer_move_as_int == 1:
computer = "S"
else:
computer = "P"
print("I play", computer)
if user == computer:
print("it's a tie")
elif (user == "R" and computer == "S") or (user == "S" and computer == "P") or (user == "P" and computer =="R"):
print("you win")
else:
print("I win")
print()
import random
user_wins = 0
computer_wins = 0
ties = 0
while True:
while True:
user = input("your move (R, S or P, X to stop playing): ")
if user == "R" or user == "S" or user == "P" or user == "X":
break
print("your input is not valid, try again !")
print()
if user == "X":
break
computer_move_as_int = random.randint(0, 2)
if computer_move_as_int == 0:
computer = "R"
elif computer_move_as_int == 1:
computer = "S"
else:
computer = "P"
print("I play", computer)
if user == computer:
print("it's a tie")
ties = ties + 1
elif (user == "R" and computer == "S") or (user == "S" and computer == "P") or (user == "P" and computer =="R"):
print("you win")
user_wins = user_wins + 1
else:
print("I win")
computer_wins = computer_wins + 1
print()
print()
print("the final result is:")
print(" you won", user_wins, "games")
print(" I won", computer_wins, "games")
print(" we had", ties, "ties")
print()
print("so the overall winner is: ", end="")
if user_wins > computer_wins:
print("you !")
elif user_wins < computer_wins:
print("me !")
else:
print("the two of us !")