print("Hi im a PC and my name is Micro, What's your name?")
name = raw_input("")
print("Hi " + name + " how are you, are you good?")
answer = (raw_input(""))
if answer == "yes":
print("That's good to hear!")
elif answer == "no":
print("Oh well")
while answer != ("yes","no")
print("Sorry, you didnt answer the question properly, Please answer with a yes or no.")
print"I'm going to sleep for 5 seconds and then i'll be back."
import time
time.sleep(5)
print"I'm back!"
need to create a loop for the yes or no bit, anyone know how? thanks for any help!
Will Vousden
33.6k9 gold badges89 silver badges97 bronze badges
-
1It's okey to be new at something. But may I suggest you look thru these serie of videos: youtube.com/watch?v=tKTZoB2Vjuk&ob=av3e or something similar? That will take you a few hours but it will be totally worth it. You will get some basic understanding of how things work :)Niclas Nilsson– Niclas Nilsson2012年01月07日 09:44:36 +00:00Commented Jan 7, 2012 at 9:44
2 Answers 2
Use while True: and when you want to stop the loop use break.
This would be your code than:
...
while True:
answer = (raw_input(""))
if answer == "yes":
print("That's good to hear!")
break
elif answer == "no":
print("Oh well")
break
else:
print("Sorry, you didnt answer the question properly, Please answer with a yes or no.")
...
answered Jan 7, 2012 at 9:25
evotopid
5,4493 gold badges29 silver badges41 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
user1135707
sorry im really new and although iv heard of both of these i havent yet learnt them. would you care to explain or give an example
And now for a completely different thing:
options = {'intro':"Hi, I'm a PC and my name is Micro, What's your name? > ",
'ask': "Hi %s how are you, are you good? > ",
'yes': "That's good to hear!",
'no': "Oh well",
'error':"Sorry, you didnt answer the question properly\n",
'hint': "Please answer with yes/no"}
name = raw_input(options['intro'])
while True:
try:
answer = raw_input(options['ask'] % name)
print options[answer.lower()]
break
except KeyError:
print options['error'], options['hint']
As you said you are a noob in Python, I wanted to introduce here several new things to complement the other answer you may find useful.
answered Jan 7, 2012 at 11:20
joaquin
86k31 gold badges146 silver badges155 bronze badges
Comments
lang-py