0

I'm trying to program a really simple code as I'm starting in Python. The problem I have is that I'm trying to loop a while if the input has no the admitted answers. What I want from my program is that depending on a "Yes / No" answer print a line. For example if you answer in an input with Yes, print Ok. If you answer with No - Ok, you said No. And if you answer with any other answer it come back to the input again. I've tried with getting the while in a function and then placing a return after the option that isn't Yes or No (I mean any other that isn't Yes or no) but it doesn't work, it doesn't even print the thrid option (the option that tells you not to use any other word that isn't yes or no). I've been trying to solve by myself this doubt reading manuals and other posts from here, but I just can't figure out what I'm doing wrong. Thanks!
Here is my code:

 def myFunction():
    while True:
        if answer == "yes":
            print("Ok.")
            break
        elif answer == "no":
            print("Ok, you said no.")
            break
        else:
            print("Please, use yes or no.")
    return answer
Txni.26
  • 43
  • 1
  • 4
  • 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) – G. Anderson Mar 11 '21 at 15:58
  • What is answer? Did you mean to have it as a parameter in your function? – Have a nice day Mar 11 '21 at 15:59

5 Answers5

0

The Return command will only work in a function.

According to the docs:

return may only occur syntactically nested in a function definition, not within a nested class definition.
Ismail Hafeez
  • 730
  • 3
  • 10
0

Lets say answer is the variable of the input. var = (answer.get()) is the line you want. then you do the exact same thing you did but like this:

var = (answer.get())
while True:
        if var == "yes":
            print("Ok.")
            break
        elif var == "no":
            print("Ok, you said no.")
            break
        else:
            print("Please, use yes or no.")
    return answer

I hope it works!

Luca Janss
  • 81
  • 10
0

Answer is not defined, and the function has no input, have a look to this modification of your code to see if it helps.

def myFunction(answer):
  while True:
    if answer == "yes":
      print("Ok.")
      break
    elif answer == "no":
      print("Ok, you said no.")
      break
    else:
      print("Please, use yes or no.")
      break

myFunction ("215456")
Reza Rahemtola
  • 1,182
  • 7
  • 16
  • 30
Sergio
  • 1
0

If you are going to generalize this in a function, you should separate the responsibility of obtaining a valid response from processing of the response.

# Python 3.8+ for walrus operator

def ask(question,validAnswers=("yes","no")):
    while (answer := input(question)) and answer not in validAnswers:
        print(f"Please, answer with {validAnswers}: ")
    return answer

r = ask("Do you want some tea? ")
if r == "yes":
    print("We have some Earl Grey")
else:
    print("No tea then.")
Alain T.
  • 40,517
  • 4
  • 31
  • 51
0

    opcao = input("welcome wants to play (yes/no):")
    
    if opcao == "yes":
        print("nWelcome to the game")
        print("n fill in your name to play:")
        name = input("welcome to the game" + name)
        print("welcome to the game" + name)
    elif opcao == "no":
      print("thank you and come back always")
    
    else:
        print("answer correctly with yes or no!!!")