| | |
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:
Solved Threads: 0
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:
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!"
Python Syntax (Toggle Plain Text)
while True: response = raw_input(" Are you ready? ") if response.lower() == "yes": print ("Then lets get started!") break elif response.lower()=="no": print ("Please try again later....") break else: 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!"
Try:
Hope it works!
Python Syntax (Toggle Plain Text)
import time response=raw_input("Are you ready?\n") while(response.lower()!="no"): if(response.lower()!="yes"): time.sleep(3) response=raw_input("\nAre you ready?\n") if(response.lower()=="yes"): raw_input("Let's get started!") break
Hope it works!
that wont work, the only problem is the while loop, what you actually want is:
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
python Syntax (Toggle Plain Text)
import time response=raw_input("Are you ready?\n") while (response.lower() != "yes"): if (response.lower() = "no"): time.sleep(3) response=raw_input("\nAre you ready?\n") raw_input("let's get started!")
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
'it is better to fight for something than to live for nothing'General George S Patton
•
•
•
•
that wont work, the only problem is the while loop, what you actually want is:
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 onpython Syntax (Toggle Plain Text)
import time response=raw_input("Are you ready?\n") while (response.lower() != "yes"): if (response.lower() = "no"): time.sleep(3) response=raw_input("\nAre you ready?\n") raw_input("let's get started!")
![]() |
Similar Threads
- Render Loop & Game Loop? (Game Development)
- Display client's PC Date & Time (ASP.NET)
- Two loop run together at a time (C++)
- Help with a date loop & error '80020009' (ASP)
- arrange date&time column (MySQL)
- sentinel controlled while loop (C++)
- QBasic Nested for loop (Legacy and Other Languages)
- No "Data & Time" Function (Windows 95 / 98 / Me)
- Clear getchar in a loop (C)
Other Threads in the Python Forum
- Previous Thread: How to find a file from a directory by Python?
- Next Thread: writing an output
| Thread Tools | Search this Thread |
address anydbm app beginner changecolor cipher class clear conversion coordinates corners curves definedlines development dictionary dynamic events examples excel feet file float format function generator getvalue gui handling homework images import input ip java keycontrol line linux list lists loan loop maintain matching maze millimeter mouse mysqldb number numbers output parsing path port prime programming projects py2exe pygame pymailer python queue random rational raw_input recursion recursive scrolledtext searchingfile shebang singleton slicenotation split string strings table tails terminal text thread threading time tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 valueerror variable variables vigenere web word wx.wizard wxpython xlwt






