954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to use loop & time with raw_input

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:

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!"

kiddo39
Junior Poster in Training
50 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Try:

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! :)

sravan953
Posting Whiz in Training
243 posts since May 2009
Reputation Points: 2
Solved Threads: 30
 

that wont work, the only problem is the while loop, what you actually want is:

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!")

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

leegeorg07
Posting Pro in Training
428 posts since Jul 2008
Reputation Points: 35
Solved Threads: 32
 

that wont work, the only problem is the while loop, what you actually want is:

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!")

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! ;)

sravan953
Posting Whiz in Training
243 posts since May 2009
Reputation Points: 2
Solved Threads: 30
 

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.

baki100
Junior Poster in Training
79 posts since Apr 2009
Reputation Points: 12
Solved Threads: 14
 

kiddo might have to use a thread and a hook to the keyboard to get tis to go

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

Thank you guys for your replies!

kiddo39
Junior Poster in Training
50 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

@ sravan: really? i didnt think it would!

leegeorg07
Posting Pro in Training
428 posts since Jul 2008
Reputation Points: 35
Solved Threads: 32
 

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

jeugtsy
Newbie Poster
1 post since Feb 2012
Reputation Points: 7
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You