如何在Python中使用原始输入函数和if函数?

2024-04-19 12:45:11 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我对编程还很陌生,正在尝试用python编写一个简单的chatterbot程序。我写了这个代码-

while (True):
    command = raw_input(shayon)
    if command == "abarjigay":
        print ("Yahoo!")

但它不起作用。有什么问题?你知道吗


Tags: 代码程序trueinputrawif编程yahoo
2条回答

如果您使用的是python3,那么应该使用input函数。参见文档:https://www.python.org/dev/peps/pep-3111/#specification

command = raw_input(shayon)

变量shayon没有在这一行之前定义,因此程序会因NameError而崩溃。你知道吗

预先给shayon赋值,或者如果您希望实际的字母序列“shayon”作为输入提示出现在用户面前,请使用字符串文字。你知道吗

shayon = "what is your favorite color?"
while (True):
    command = raw_input(shayon)
    if command == "abarjigay":
        print ("Yahoo!")

或者

while (True):
    command = raw_input("shayon")
    if command == "abarjigay":
        print ("Yahoo!")

相关问题 更多 >