Repeating question with a loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Thekid
    New Member
    • Feb 2007
    • 145

    Repeating question with a loop

    How can I get this question to keep repeating until either a yes or no is given as an answer:

    Code:
    response = raw_input("Are you ready? ")
    if response.lower() == "yes":
         print "Alrighty then, let's begin!"
    if response.lower() == "no":
        print "Try again later when you are ready"
    Let's say you enter: maybe
    I'd like the question to keep repeating. I tried making some 'for' loops but wasn't doing it right.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Encapsulate the input in a while loop. Use an if, elif, else block instead of multiple if statements.

    Code:
    while True:
        response = raw_input("Are you ready? ")
        if response.lower() == "yes":
             print "Alrighty then, let's begin!"
             break
        elif response.lower() == "no":
            print "Try again later when you are ready"
            break
        else:
            print "Please enter 'Yes' or 'No' please."

    Comment

    • Thekid
      New Member
      • Feb 2007
      • 145

      #3
      I see. I thought about the 'while' loop too but wasn't sure how to implement it. Thanks!

      Comment

      Working...