0

I'm trying to write a piece of code with that repeats the question until the correct answer is given, with a few different responses for likely answers. This is what I have so far, but after it responds to the input given, the program moves on. I can't figure out how to do this with a while loop since it ends with a specific answer and not with else.

answer = input("What is your favorite eldest daughter's name?   ").lower()

if answer == "a" or answer == "al":
    print("Of course, we all knew that")
elif answer == "c" or answer == "chris":
    print("""Oh she must be standing right there.
But that\'s not it, try again.""")

elif answer == "e" or answer == "ed":
    print("Not even the right gender. Try again.")
elif answer == "n" or answer == "nic":
    print("Only biological children please.")

else:
    print("I'm sorry. Only children. Were you thinking of the dogs?")
ADubz
  • 3
  • 1
  • 1
  • 3

3 Answers3

1

break is what you want. Use it like so:

while 1:

    answer = input("What is your favorite eldest daughter's name?   ").lower()

    if answer == "a" or answer == "al": #assuming this is the right answer
        print("Of course, we all knew that")
        break
    elif answer == "c" or answer == "chris":
        print("""Oh she must be standing right there.
    But that\'s not it, try again.""")

    elif answer == "e" or answer == "ed":
        print("Not even the right gender. Try again.")
    elif answer == "n" or answer == "nic":
        print("Only biological children please.")

    else:
        print("I'm sorry. Only children. Were you thinking of the dogs?")
El'endia Starman
  • 2,204
  • 21
  • 35
  • Just what I needed! I didn't know you could do a break on anything besides an else statement, learned something new today. – ADubz Dec 01 '15 at 20:52
1

You basically need to keep repeating the question until the answer is considered to be acceptable by the program.

Code

#!/usr/bin/python3 -B

answers = ['alice', 'chris', 'bob']
answer  = None
while answer not in answers:
    answer = input('Enter your answer: ')

print('Your answer was: {}'.format(answer))

Basically the code here has a list of acceptable answers and it initializes the user's answer to None. Then it enters a while loop that keeps repeating itself until the user's answer is found within the list of acceptable answers.

Output

➜  ~  ./script.py
Enter your answer: alice
Your answer was: alice
➜  ~  ./script.py
Enter your answer: no
Enter your answer: no
Enter your answer: yes
Enter your answer: bob
Your answer was: bob

Improving the Code

You can now adapt the code to use the messages of your choice. Note, too, that if you want to provide a different response for any of the acceptable entries, you could use a dictionary and update the code slightly.

For example, you could have something like this:

answers = {
    'bob': 'This was the best choice',
    'alice': 'Everyone picks the hot gal',
    # ... and so on
}

Then you'd keep iterating just like before by checking the keys of the answers dictionary (e.g. while answer not in answers.keys():).

When the loop exits with an acceptable answer, you'd simply

print(answers[answer])

If answers == 'alice', then the program would print Everyone picks the hot gal.

It would significantly simplify your code and make it easier for you to understand and work with :)

code_dredd
  • 5,915
  • 1
  • 25
  • 53
0

Use the while loop, and the break statement:

while True:
    # . . .
    if correct_answer:
        break
RobertL
  • 365
  • 1
  • 9