Ask your own question, for FREE!
MIT 6.189 A Gentle Introduction to Programming Using Python (OCW) 10 Online
OpenStudy (anonymous):

Working on exercise 1.7 -- the Rock Paper Scissors game. My question: If the user types in something other than "rock," "paper, or "scissors," the program needs to loop back and ask for another input. Is there some way of using the IF command to do this? Is there some other command I am overlooking here?

OpenStudy (anonymous):

What about this ? choices = ["rock", "paper", "scissors"] choice = raw_input("Your choice: ") while choice not in choices: choice = raw_input("Please enter a valid choice (rock, paper, scissors): ")

OpenStudy (anonymous):

I just used the else statement for that, since if none of the valid combinations were present, someone didn’t write their right word. @FabienToune has written a version that asks for a correct choice already beforehand, which might be a better choice.

OpenStudy (anonymous):

That being said, I balantly copied @FabienToune’s solution as well now and changed the `else` at the end as a warning if anything goes *really* wrong ;)

OpenStudy (anonymous):

This is another example of the "not in list" syntax, which I like, but I don't think we got that in the instructions yet, so my insight was that it had to actually ask the same question twice. I used a while loop for this. It doesn't loop back so much as ask again: while again != ("n"): player1 = raw_input("Player 1?").lower() while (player1 != "rock" and player1 != "paper" and player1 != "scissors"): print "This is not a valid object selection" player1 = raw_input("Player 1?").lower()

OpenStudy (anonymous):

Thanks, I ws looking for the same thing and now I know how!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!