By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,767 Members | 2,114 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,767 IT Pros & Developers. It's quick & easy.

Repeating question with a loop

Thekid
100+
P: 145
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.
Jul 6 '09 #1
Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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."
Jul 6 '09 #2

Thekid
100+
P: 145
I see. I thought about the 'while' loop too but wasn't sure how to implement it. Thanks!
Jul 7 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.