0

I am a beginner in Python. As a step to try and learn it I am now creating a story type game.

So I want to check if the user has entered a valid input.

print("You see two roads, one to the left and one to the right...\n")
time.sleep(1)
answer = input("Do you want to go left or right?[Valid answer is yes/no\n").lower

if answer == "yes" :
      print("You start to walk left")
else:
      print("You end your journey here, thanks for playing")
      quit()

Ok, so basically the only check I get here is if the user types yes or no, but what if the user types something else? I would then love to have an option that prints out "Please type yes or no" and then sends the user the same question again??

Sorry if I did not manage to answer this in a good way, english is not my native language.

Tom Knudsen
  • 63
  • 1
  • 8
  • I am sorry, but did a search and found nothing that fits. :) – Tom Knudsen Apr 06 '17 at 23:32
  • I do not think that this is a duplicate. – Josh Apr 06 '17 at 23:35
  • @Josh - It is a perfect duplicate. Why do you believe otherwise? – TigerhawkT3 Apr 06 '17 at 23:38
  • @TigerhawkT3 - it just seems like the solutions are slightly different to this page and they may not be that helpful in this case although I can see why it can be thought of as a duplicate. – Josh Apr 06 '17 at 23:43
  • @Josh - They are not different at all. In fact, your solution is inferior, as it doesn't handle case-sensitivity or multiple possibilities like that question's top answer does. I understand that you want to participate, but answering incredibly common duplicates (this particular question is asked multiple times a day) and then arguing against the closure in an attempt to justify your answer is ___not___ the way to make a positive contribution. – TigerhawkT3 Apr 06 '17 at 23:59
  • @TigerhawkT3 - Ok. It actually does handle case-sensitivity because it converts all characters in the string to lowercase with .lower(). – Josh Apr 07 '17 at 00:03
  • @Josh - Oh I see; I didn't bother scrolling all the way to the right in your 123-character line. That still leaves all the other problems, though. Please refrain from leaving inferior answers to duplicate questions. – TigerhawkT3 Apr 07 '17 at 00:08
  • @TigerhawkT3 - My solution worked absolutely fine for the OP. He doesn't need any of the solutions on the page provided as they're not what he asked for and unnecessary in this case. – Josh Apr 07 '17 at 00:14
  • @Josh - And yet, you provided absolutely no new information not already present in that answer. That's what makes this a duplicate, and that's what makes your answer actively unhelpful. Again, please review [answer] to learn about what constitutes a useful answer. – TigerhawkT3 Apr 07 '17 at 00:20

1 Answers1

-1
answer = ""
while answer != "yes" and answer != "no":answer = input("Do you want to go left or right?[Valid answer is yes/no\n").lower()
if answer == "yes" :
    print("You start to walk left")
else:
    print("You end your journey here, thanks for playing")
    quit()

This uses a while loop that keeps iterating until the variable is equal to either 'yes' or 'no'.

Josh
  • 114
  • 3
  • Thank you very much, I will try that for sure! "voted" – Tom Knudsen Apr 06 '17 at 23:32
  • 2
    As stated in [answer], please avoid answering unclear, overly-broad, typo, opinion-based, unreproducible, or duplicate questions. Write-my-code requests and low-effort homework questions are off-topic for [so] and more suited to professional coding/tutoring services. Good questions adhere to [ask], include a [mcve], have research effort, and have the potential to be useful to future visitors. Answering inappropriate questions harms the site by making it more difficult to navigate and encouraging further such questions, which can drive away other users who volunteer their time and expertise. – TigerhawkT3 Apr 06 '17 at 23:40