Reputation: 1
So i'm making a rock paper game,I have added my source code. So far I have made the players enter their choices. I tried adding a check to make sure the choices they enter is one of the three. My program corrects it once, and then it stops doing anything
Here is my source code.
print('''Please pick one of the following:
Rock
Paper
Scissors''')
p1 = None
p2 = None
while True:
gameDict = {"rock":1,"paper":2,"scissors":3}
in1 = input("Player 1: ").lower()
in2 = input("Player 2: ").lower()
p1 = gameDict.get(in1)
p2 =gameDict.get(in2)
while gameDict.get(p1)==None or gameDict.get(p2)==None:
if(p1==None):
p1=input("Player 1, please enter one of the choices listed above: ")
elif p2== None:
p2=input("Player 2, please enter one of the choices listed above: ")
print('Done!!')
print(p1,p2)
Upvotes: 0
Views: 113
Reputation: 445
It's not that it's not doing anything. In fact it's doing a lot, it's in an infinite loop. What happens when a wrong input is entered is:
p1 = None
, p2 = None
.p1 == None
evaluates to true
, it executes the if
statement, a new value is assigned to p1
and now it's not None
anymore.p2 == None
evaluates to true
, it executes the if
statement, a new value is assigned to p2
and now it's not None
anymore.p1
and p2
are not None
, and thus none of the if
statements gets executed and the loop iterates infinitely.I suggest you do the following:
print('''Please pick one of the following:
Rock
Paper
Scissors''')
p1 = None
p2 = None
while True:
gameDict = {"rock":1, "paper":2, "scissors":3}
in1 = input("Player 1: ").lower()
in2 = input("Player 2: ").lower()
p1 = gameDict.get(in1)
p2 = gameDict.get(in2)
while p1 ==None or p2 ==None:
if(p1 == None):
val = input("Player 1, please enter one of the choices listed above: ")
if(gameDict.get(val) != None):
p1 = val
if p2 == None:
val = input("Player 2, please enter one of the choices listed above: ")
if(gameDict.get(val) != None):
p2 = val
print('Done!!')
print(p1, p2)
Stuff I have fixed:
val
.elif
to if
because either of the players' input can be valid, but not the other and you would want to loop until both are valid.Upvotes: 2
Reputation: 367
i think this is the right code: (you should check if either p1 or p2 is None)
print('''Please pick one of the following:
Rock
Paper
Scissors''')
p1 = None
p2 = None
while True:
gameDict = {"rock":1,"paper":2,"scissors":3}
in1 = input("Player 1: ").lower()
in2 = input("Player 2: ").lower()
p1 = gameDict.get(in1)
p2 =gameDict.get(in2)
while p1==None or p2==None:
if(p1==None):
p1=input("Player 1, please enter one of the choices listed above: ")
elif p2== None:
p2=input("Player 2, please enter one of the choices listed above: ")
print('Done!!')
print(p1,p2)
Upvotes: 0
Reputation: 57033
You compare keys entered by the user ("scissors") to the values in the dictionary. You need to ensure that the keys are right:
while in1 not in gameDict:
in1 = input("Player 1, please...: ").lower()
while in2 not in gameDict:
in2 = input("Player 2, please...: ").lower()
Once you get valid inputs, you can look them up:
p1 = gameDict[in1]
p2 = gameDict[in2]
Upvotes: 0