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

Recommended Answers

All 8 Replies

Member Avatar for sravan953

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

commented: Thanks! +1
Member Avatar for leegeorg07

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

commented: Thanks! +1
Member Avatar for sravan953

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

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.

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

Thank you guys for your replies!

Member Avatar for leegeorg07

@ sravan: really? i didnt think it would!

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

commented: Difficult to understand, warming up old post -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.