我是編程新手。根據我的書,這段代碼應該會出錯。Python崩潰過程:使用int()接受數值輸入
>>> age = input("How old are you? ")
How old are you? 21
>>> age >= 18
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
vTypeError: unorderable types: str() >= int()
在卓異我保存的一個.py文件:This is my .py file.
然後,在終端當我試圖運行它。它沒有給我一個錯誤。 第一次,我跑進了21歲,沒有任何郵票,它返回True。 然後,我進入17,它返回False。
首先,我的電腦如何知道它們是整數?我沒有輸入age = int(年齡)。
其次,當我輸入「21」時,它返回True。沒有錯誤。爲什麼會發生?
它如何比較字符串和整數?
當我輸入「17」作爲我的年齡,它再次返回True。 **爲什麼會發生這種情況?
它不僅比較刺和一個整數,但給出錯誤的答案也是這個時間。**
This is the screenshot of my terminal window
您正在使用python2並且該書正在使用python3,python 2中的輸入等效於'eval(raw_input())' –
請參閱http://stackoverflow.com/a/4915366/1222951 –