I am fairly new to python and am currently making a tic-tac-toe game.
I am trying to make it so if your input does not equal rock, paper, or scissors it will prompt you to re enter until you get it right, although when I run the code it asks you to re enter one time and then continues on, regardless of what you enter.
I have tried looking around the internet but nothing seems to be what I am looking for, here is my code:
import random
print("How many times would you like to play?")
repeat = input()
possible_choices = ["rock", "paper", "scissors"]
for x in range (int(float(repeat))):
user_input = input("Enter a choice (rock, paper, scissors): ")
if user_input != possible_choices:
user_input = False
else:
user_input = True
while user_input == False:
user_input = input("Please enter rock, paper, or scissors: ")
if user_input == possible_choices:
user_input = True
computer_action = random.choice(possible_choices)
print(f"\nYou chose {user_input}, computer chose {computer_action}.\n")
if (computer_action == user_input):
print(f"You both chose {user_input} it's a tie!")
if (user_input == "rock") and (computer_action == "paper"):
print("Paper beats rock, you lose!")
if (user_input == "rock") and (computer_action == "scissors"):
print("Rock beats scissors, you win!")
if (user_input == "paper") and (computer_action == "scissors"):
print("Scissors beats paper, you lose!")
if (user_input == "scissors") and (computer_action == "paper"):
print("Scissors beats paper, you win!")
print("")
if (user_input == "paper") and (computer_action == "rock"):
print("Paper beats rock, you win!")
print("Game ended.")
>Solution :
You redefine user_input inside the for loop. Since a string will evaluate to not False the loop will always break. Another thing, you should be checking if the input is in the possible choices, not equal to the whole list.
for x in range (int(repeat)):
user_input = input("Enter a choice (rock, paper, scissors): ")
valid_input = False
if user_input in possible_choices:
valid_input = True
while valid_input == False:
user_input = input("Please enter rock, paper, or scissors: ")
if user_input in possible_choices:
valid_input = True