Python 算命师名单和原始输入
这是我现在的python代码,如果用户回答“是”,我在循环这个问题时遇到了问题。当用户回答“否”时,它仍然会询问他们的问题是什么。这是非常令人沮丧的,因为我是一个初学者,我不能为我的生活,这一点。以下是我的代码:Python 算命师名单和原始输入,python,Python,这是我现在的python代码,如果用户回答“是”,我在循环这个问题时遇到了问题。当用户回答“否”时,它仍然会询问他们的问题是什么。这是非常令人沮丧的,因为我是一个初学者,我不能为我的生活,这一点。以下是我的代码: import random answers = ["Of course", "No freaking way man", "I don't think so", "Maybe", "Get out of here with that nonsense, no way!"]
import random
answers = ["Of course", "No freaking way man", "I don't think so", "Maybe", "Get out of here with that nonsense, no way!"]
question = 'What is your question? '
name=raw_input('What is your name : ')
print ("Hi %s! I am fortune teller Idris" % name)
def prompt_question(question):
response = raw_input(question)
print random.choice(answers)
prompt_question(question)
def ask_again():
response2 = raw_input('Do you Have another question? ')
if response2 == 'Yes' or 'yes' or 'Y':
print prompt_question(question)
elif response2 == 'No' or response2 == 'no':
print "goodbye"
else:
print 'You need to say yes or no'
ask_again()
我能得到的任何帮助都会很好,谢谢 非空字符串返回True。因此,
如果“是”:
将始终被接受。你需要使用
if response2 in ['Yes', 'yes', 'Y']:
或
Edit:我将编写一个main()
函数
def ask_again():
while True:
response2 = raw_input('Do you Have another question? ')
if response2 in ['Yes', 'yes', 'Y']:
return True
elif response2 in ['No', 'no', 'N']:
return False
else:
print 'You need to say yes or no'
def main():
while True:
prompt_question(question)
if not ask_again():
print "goodbye"
break
非空字符串返回True。因此,
如果“是”:
将始终被接受。你需要使用
if response2 in ['Yes', 'yes', 'Y']:
或
Edit:我将编写一个main()
函数
def ask_again():
while True:
response2 = raw_input('Do you Have another question? ')
if response2 in ['Yes', 'yes', 'Y']:
return True
elif response2 in ['No', 'no', 'N']:
return False
else:
print 'You need to say yes or no'
def main():
while True:
prompt_question(question)
if not ask_again():
print "goodbye"
break
哎呀:
更好:
if response2 == 'Yes' or response2 == 'yes' or response2 == 'Y':
“anything”或“yes”将始终为true,因为只有空字符串(“”)为false。或者评估其中的每一件事情,而不是像英语中那样在列表中选择
因此,在您的代码中,机器从不尝试查看响应是否为“否”。它只是假设它应该问另一个问题,因为“是”是真的 oops:
更好:
if response2 == 'Yes' or response2 == 'yes' or response2 == 'Y':
“anything”或“yes”将始终为true,因为只有空字符串(“”)为false。或者评估其中的每一件事情,而不是像英语中那样在列表中选择
因此,在您的代码中,机器从不尝试查看响应是否为“否”。它只是假设它应该问另一个问题,因为“是”是真的 第一个选项更像pythonic,因为它更容易写和读。另外,ask_中的else条件当前也退出“循环”太好了,可以了!但是,在我回答“是”之后,我如何才能循环不断地问一个问题呢。因为在打印提示问题(问题)之后,它一问我的另一个问题,代码就结束了。在任何类型的循环中都没有
再次询问
,这就是它不循环的原因@IBREWSTER唯一的问题是当我键入“否”时,它只会问我“你的问题是什么”。我想让它打印再见并退出系统。第一个选项更具pythonic风格,因为它更易于写入和读取。此外,ask_中的else条件当前再次退出“循环”太好了,可以了!但是,在我回答“是”之后,我如何才能循环不断地问一个问题呢。因为在打印提示问题(问题)之后,它一问我的另一个问题,代码就结束了。在任何类型的循环中都没有再次询问
,这就是它不循环的原因@IBREWSTER唯一的问题是当我键入“否”时,它只会问我“你的问题是什么”。我想让它打印并退出系统。