| | |
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 |
Tag cloud for Python
abrupt ansi anti apache approximation array basic beginner book builtin calculator chmod code converter countpasswordentry curved dan08 dictionaries dictionary dynamic examples excel file filename float format ftp function gui heads homework import inches input java launcher library line lines linux list lists loop mouse mysql mysqlquery number numbers numeric output parsing path phonebook plugin port prime programming progressbar projects py2exe pygame pyqt pysimplewizard python random recursion recursive redirect refresh scrolledtext server software ssh stamp statictext statistics string strings table terminal text textarea thread threading time tkinter tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable windows wordgame wxpython






