4 回答

TA貢獻1854條經(jīng)驗 獲得超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條經(jīng)驗 獲得超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
但是,請不要相信我的意思。...打開解釋器,對問題的一小部分進行試驗,您可以立即使用它。
添加回答
舉報