0

I'm designing a code that recreates the same logic as a flowchart. I've made a function that allows you to interact with the questions and it works fine, until you input something other than 'yes' or 'no'. I want to create a function that, for every input answer, if it is different than yes and no, it should say to input it again, and then keep going from where it left off.

So I tried

    def error(x):
    while True:
        if x.lower() != "yes" or x.lower() != "no":
            tryagain = input("Please only enter 'yes' or 'no'\n")
            return (error(tryagain))
        continue

But after asking me to input the answer again, even if I type yes or no, it just keeps on repeating "Please only enter 'yes' or 'no'". I've also tried other codes (for loops, ifs and elifs, while loops worded differently) but they don't work. I'm currently working without packages, as I'm still learning the basics.

Please help

Mary
  • 153
  • 1
  • 2
  • 10
  • 2
    Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – kaya3 Dec 04 '19 at 01:38
  • The replies to that question either use packages or try/except, but I do not get an error message if I don't enter yes or no, the program stops working or keeps on asking me the same question – Mary Dec 04 '19 at 01:42
  • Try `and` instead of `or`? Alternatively, `if x.lower() not in ["yes", "no"]:` – Green Cloak Guy Dec 04 '19 at 01:44
  • The accepted answer is broad and covers a wide range of use cases; yours is under *"Implementing Your Own Validation Rules"*. – kaya3 Dec 04 '19 at 01:44

1 Answers1

1

Your code has a continue statement at the wrong place. When the questions are answered with yes or no your program should continue to next step. However when user enters anything else it should prompt to enter input with 'yes' or 'no'

You can try this

   def error(x):
       while True:
           if (x.lower() == "yes" or x.lower() == "no"):
              break
           else:
              tryagain = input("Please only enter 'yes' or 'no'\n")
              return (error(tryagain))

It should work.

Yugesha Sapte
  • 105
  • 1
  • 1
  • 9