943,865 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 2729
  • Python RSS
Jul 13th, 2009
0

how to use loop & time with raw_input

Expand Post »
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:

Python Syntax (Toggle Plain Text)
  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!"
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
kiddo39 is offline Offline
50 posts
since Nov 2008
Jul 14th, 2009
1

Re: how to use loop & time with raw_input

Try:

Python Syntax (Toggle Plain Text)
  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!
Reputation Points: 2
Solved Threads: 30
Posting Whiz in Training
sravan953 is offline Offline
242 posts
since May 2009
Jul 14th, 2009
1

Re: how to use loop & time with raw_input

that wont work, the only problem is the while loop, what you actually want is:
python Syntax (Toggle Plain Text)
  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
Reputation Points: 35
Solved Threads: 32
Posting Pro in Training
leegeorg07 is offline Offline
428 posts
since Jul 2008
Jul 14th, 2009
0

Re: how to use loop & time with raw_input

Click to Expand / Collapse  Quote originally posted by leegeorg07 ...
that wont work, the only problem is the while loop, what you actually want is:
python Syntax (Toggle Plain Text)
  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!
Reputation Points: 2
Solved Threads: 30
Posting Whiz in Training
sravan953 is offline Offline
242 posts
since May 2009
Jul 14th, 2009
0

Re: how to use loop & time with raw_input

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.
Reputation Points: 12
Solved Threads: 14
Junior Poster in Training
baki100 is offline Offline
79 posts
since Apr 2009
Jul 14th, 2009
0

Re: how to use loop & time with raw_input

kiddo might have to use a thread and a hook to the keyboard to get tis to go
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Jul 14th, 2009
0

Re: how to use loop & time with raw_input

Thank you guys for your replies!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
kiddo39 is offline Offline
50 posts
since Nov 2008
Jul 15th, 2009
0

Re: how to use loop & time with raw_input

@ sravan: really? i didnt think it would!
Reputation Points: 35
Solved Threads: 32
Posting Pro in Training
leegeorg07 is offline Offline
428 posts
since Jul 2008
6 Days Ago
-1

looop

when he input N when the computer say "do you have another name"then he finish. but if he input Y then it keep repiting the question
As Integer

console.writeline("please enter you name")
name = console.readline()
Do

Loop

can someone tell me what wrong with this


If
response = raw_input("do you have another name?\n")

If (response.lower() = "Y") Then
response = raw_input("/please enter other name\n")
ElseIf response = raw_input("thank you") Then
Reputation Points: 7
Solved Threads: 0
Newbie Poster
jeugtsy is offline Offline
1 posts
since Feb 2012

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: class Trieda
Next Thread in Python Forum Timeline: can wxpython can become a web base program?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC