我工作过的回答这样一个问题:Python Interactive Shell Type Application输入与raw_input:Python交互式Shell应用程序?
我的代码看起来像这样
def main():
while True:
s = input('> ')
if s == 'hello':
print('hi')
if s == 'exit':
break
if __name__ == "__main__":
main()
如果我运行它,然后键入你好,我得到
File "<string>", line 1, in <module>
NameError: name 'hello' is not defined
应该如何我正在听文字,并根据结果调用不同的功能?
你如何运行它? – 2013-04-05 18:10:54
python my_shell.py在终端 – 2013-04-05 18:12:04
您需要['raw_input'](http://docs.python.org/2/library/functions.html#raw_input),而不是['input'](http:// docs。 python.org/2/library/functions.html#input)(后者评估你输入的Python表达式)。 – 2013-04-05 18:13:56