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!"
需要为是或否位创建一个循环,任何人都知道如何? 谢谢你的帮助!
答案 0 :(得分:1)
使用while True:
,当您想要停止循环时,请使用break
。
这将是您的代码:
...
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.")
...
答案 1 :(得分:1)
现在换一个完全不同的东西:
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']
正如你所说的你是Python中的菜鸟,我想在这里介绍几个新的东西来补充你可能会觉得有用的其他答案。