how to use loop & time with raw_input

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2008
Posts: 43
Reputation: kiddo39 is an unknown quantity at this point 
Solved Threads: 0
kiddo39 kiddo39 is offline Offline
Light Poster

how to use loop & time with raw_input

 
0
  #1
Jul 13th, 2009
Hi, I'm trying to run code that asks a yes or no question. If a yes or no isn't entered, then the question will continue to re-ask say every 3 seconds until a yes or no is entered. Here's the basics of it:

  1. while True:
  2. response = raw_input(" Are you ready? ")
  3. if response.lower() == "yes":
  4. print ("Then lets get started!")
  5. break
  6. elif response.lower()=="no":
  7. print ("Please try again later....")
  8. break
  9. else:
  10. print ("Please enter a 'yes' or 'no' response!")

With that, I can add another while True: after the last 'else' statement but that's not exactly what I'm after. Instead, I'd like the first question "Are you ready?" to keep repeating until 'yes' or 'no' is entered. Can I somehow incorporate sleep() in this and how can I get the raw_input question to keep repeating? Example output:

>>> Are you ready? maybe
>>>Are you ready?
(3 second delay, if no answer ask again)
>>>Are you ready?
(3 second delay, if no answer ask again)
>>>Are you ready?
(3 second delay, if no answer ask again)
>>>Are you ready?yes
"Then lets get started!"
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 231
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training

Re: how to use loop & time with raw_input

 
1
  #2
Jul 14th, 2009
Try:

  1. import time
  2.  
  3. response=raw_input("Are you ready?\n")
  4.  
  5. while(response.lower()!="no"):
  6. if(response.lower()!="yes"):
  7. time.sleep(3)
  8. response=raw_input("\nAre you ready?\n")
  9. if(response.lower()=="yes"):
  10. raw_input("Let's get started!")
  11. break

Hope it works!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 396
Reputation: leegeorg07 is an unknown quantity at this point 
Solved Threads: 31
leegeorg07's Avatar
leegeorg07 leegeorg07 is offline Offline
Posting Whiz

Re: how to use loop & time with raw_input

 
1
  #3
Jul 14th, 2009
that wont work, the only problem is the while loop, what you actually want is:
  1. import time
  2.  
  3. response=raw_input("Are you ready?\n")
  4.  
  5. while (response.lower() != "yes"):
  6. if (response.lower() = "no"):
  7. time.sleep(3)
  8. response=raw_input("\nAre you ready?\n")
  9. raw_input("let's get started!")
all i did was change the loop so that you didnt need to have the code for checking if it is yes and then it will simply break to carry on
don't judge me because I'm a year 8!

'it is better to fight for something than to live for nothing'General George S Patton
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 231
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training

Re: how to use loop & time with raw_input

 
0
  #4
Jul 14th, 2009
Originally Posted by leegeorg07 View Post
that wont work, the only problem is the while loop, what you actually want is:
  1. import time
  2.  
  3. response=raw_input("Are you ready?\n")
  4.  
  5. while (response.lower() != "yes"):
  6. if (response.lower() = "no"):
  7. time.sleep(3)
  8. response=raw_input("\nAre you ready?\n")
  9. raw_input("let's get started!")
all i did was change the loop so that you didnt need to have the code for checking if it is yes and then it will simply break to carry on
Mine works too, I tested it!
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 47
Reputation: baki100 is an unknown quantity at this point 
Solved Threads: 10
baki100 baki100 is offline Offline
Light Poster

Re: how to use loop & time with raw_input

 
0
  #5
Jul 14th, 2009
it works but not what kiddo asked for, the application that he/she wants, wants to check for keyboard inactivity and if 3 seconds passed it prints the question 'Are you ready yet' again.
Last edited by baki100; Jul 14th, 2009 at 10:16 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 176
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: how to use loop & time with raw_input

 
0
  #6
Jul 14th, 2009
kiddo might have to use a thread and a hook to the keyboard to get tis to go
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 43
Reputation: kiddo39 is an unknown quantity at this point 
Solved Threads: 0
kiddo39 kiddo39 is offline Offline
Light Poster

Re: how to use loop & time with raw_input

 
0
  #7
Jul 14th, 2009
Thank you guys for your replies!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 396
Reputation: leegeorg07 is an unknown quantity at this point 
Solved Threads: 31
leegeorg07's Avatar
leegeorg07 leegeorg07 is offline Offline
Posting Whiz

Re: how to use loop & time with raw_input

 
0
  #8
Jul 15th, 2009
@ sravan: really? i didnt think it would!
don't judge me because I'm a year 8!

'it is better to fight for something than to live for nothing'General George S Patton
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC