4 回答

TA贡献1854条经验 获得超8个赞
您想测试字符串的相等性"exit",所以不要将其转换为int
text = raw_input("Write exit here: ")
if text == "exit":
print "Exiting!"
else:
print "Not exiting!"
input==exitinput与exit可能使您感到困惑的功能相比。如果您尝试过,input == Exit它甚至不会运行。

TA贡献1843条经验 获得超7个赞
Python是一种脚本语言-交互式地运行python(只需键入python)或运行调试器(如idle,eric,komodo(等等))非常容易,并且可以使用它。在这里,我尝试了一些组合,以查看哪些有效,哪些无效:
>>> raw_input("write exit here: ")
write exit here: exit
'exit'
>>> my_input = raw_input("write exit here: ")
write exit here: exit
>>> my_input
'exit'
>>> my_input = int(raw_input("wite exit here: "))
wite exit here: exit
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
my_input = int(raw_input("wite exit here: "))
ValueError: invalid literal for int() with base 10: 'exit'
>>> my_input == exit
False
>>> type(exit)
<class 'site.Quitter'>
>>> my_input == "exit"
True
但是,请不要相信我的意思。...打开解释器,对问题的一小部分进行试验,您可以立即使用它。
添加回答
举报