Open study

is now brainly

With Brainly you can:

  • Get homework help from millions of students and moderators
  • Learn how to solve problems with step-by-step explanations
  • Share your knowledge and earn points by helping other students
  • Learn anywhere, anytime with the Brainly app!

A community for students.

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?

MIT 6.189 A Gentle Introduction to Programming Using Python (OCW)
See more answers at brainly.com
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Join Brainly to access

this expert answer

SIGN UP FOR FREE
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): ")
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.
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 ;)

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

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()
Thanks, I ws looking for the same thing and now I know how!

Not the answer you are looking for?

Search for more explanations.

Ask your own question