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

4
  • 2
    Does this answer your question? Asking the user for input until they give a valid response Commented Dec 4, 2019 at 1: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 Commented Dec 4, 2019 at 1:42
  • Try and instead of or? Alternatively, if x.lower() not in ["yes", "no"]: Commented Dec 4, 2019 at 1:44
  • The accepted answer is broad and covers a wide range of use cases; yours is under "Implementing Your Own Validation Rules". Commented Dec 4, 2019 at 1:44

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

When I input yes or no it just doesn't return anything else
yes, that is because we are writing continue in case the user inputs 'yes' or 'no'. you should use break instead if you want to go back to the previous code where you are calling error(x) function.
Edited my answer as per your requirement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.