Connecting Tech Pros Worldwide Forums | Help | Site Map

Repeating question with a loop

Thekid's Avatar
Member
 
Join Date: Feb 2007
Posts: 110
#1: Jul 7 '09
How can I get this question to keep repeating until either a yes or no is given as an answer:

Expand|Select|Wrap|Line Numbers
  1. response = raw_input("Are you ready? ")
  2. if response.lower() == "yes":
  3.      print "Alrighty then, let's begin!"
  4. if response.lower() == "no":
  5.     print "Try again later when you are ready"
  6.  
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's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,566
#2: Jul 7 '09

re: Repeating question with a loop


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

Expand|Select|Wrap|Line Numbers
  1. while True:
  2.     response = raw_input("Are you ready? ")
  3.     if response.lower() == "yes":
  4.          print "Alrighty then, let's begin!"
  5.          break
  6.     elif response.lower() == "no":
  7.         print "Try again later when you are ready"
  8.         break
  9.     else:
  10.         print "Please enter 'Yes' or 'No' please."
Thekid's Avatar
Member
 
Join Date: Feb 2007
Posts: 110
#3: Jul 7 '09

re: Repeating question with a loop


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