我工作過的回答這樣一個問題: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