I am new to Python and I am doing some projects to try and learn it. I am trying to make a Rock Paper Scissors game to learn conditions and loops. I have a function in my game called "validator" that has a while loop with 4 string conditions however when I try to reassign that variable to break the loop it still gives me my error message that I built in saying "Please enter a valid response".
How do I get the while loop to end and return the variable ans?
Thank you for your help,
-Crow
# Rock Paper Scissors Game
import random
# Makes the Player Choose a Valid Answer Function
def validator():
ans = input()
while ans != "Rock" or "Paper" or "Scissors" or "QUIT":
ans = input("Please enter a valid response: ")
else:
return ans
# Comp Choosers Function
def compcho():
v = random.randrange(1, 3)
if v == 1:
return "Rock"
if v == 2:
return "Paper"
if v == 3:
return "Scissors"
# Win decider
def decider(man, pc):
if man == pc:
return "It's a tie! " + man + " to " + pc + "!"
elif man != pc:
if man == "Rock" and pc == "Scissors":
return "The player's choice of " + man + " beats the PC's choice of " + pc + "! Player Victory!"
if man == "Rock" and pc == "Paper":
return "The player's choice of " + man + " loses to the PC's choice of " + pc + ". Player Defeat."
if man == "Scissors" and pc == "Rock":
return "The player's choice of " + man + " loses to the PC's choice of " + pc + ". Player Defeat."
if man == "Scissors" and pc == "Paper":
return "The player's choice of " + man + " beats the PC's choice of " + pc + "! Player Victory!"
if man == "Paper" and pc == "Rock":
return "The player's choice of " + man + " beats the PC's choice of " + pc + "! Player Victory!"
if man == "Paper" and pc == "Scissors":
return "The player's choice of " + man + " loses to the PC's choice of " + pc + ". Player Defeat."
if man == "Quit":
print("Goodbye")
else:
print("Hmm I wasn't expecting " + man + ".")
# Program Start
print("Welcome to the Rock Paper Scissors Game!")
choice = "ham"
while choice != "Quit":
# Chooser
print("Please Enter Rock Paper Scissors or Quit")
valans = validator()
pcpick = compcho()
print(decider(valans, pcpick))
You can write your condition like this (repeat the variable name and check your logical operators) :
while ans != "Rock" and ans != "Paper" and ans != "Scissors" and ans != "QUIT":
or use 'in' :
while ans not in ["Rock","Paper","Scissors","QUIT"] :