我從Pyhton教程手冊中複製並粘貼了這些代碼行。爲什麼當我嘗試在PyCharm中運行它時,這段代碼不起作用?在Python中引發我自己的異常2.7
def inputNumber():
x = input ('Pick a number: ')
if x == 17:
raise 'BadNumberError', '17 is a bad number'
return x
inputNumber()
這是我當我運行代碼:
Pick a number: 17
Traceback (most recent call last):
File "C:/Users/arman/Desktop/Scribble/Hello.py", line 153, in <module>
inputNumber()
File "C:/Users/arman/Desktop/Scribble/Hello.py", line 151, in inputNumber
raise 'BadNumberError', '17 is a bad number'
TypeError: exceptions must be old-style classes or derived from BaseException, not str
該教程是*非常過時*的然後。你想選擇一個不同的教程。代碼在不產生棄用警告的情況下工作的最後一個Python版本是Python 2.2(2001年發佈),支持從2.6(2008)完全刪除。使用概念和語法的教程*遠遠落後於時代*也會有其他問題。找到比〜15歲更新的東西。 –
另外,如果您剛剛開始使用Python,我建議您從Python 3開始,而不是2. Python 2已停止使用,2.7將僅在2020年之前收到安全修復程序。Python 3是所有能源都要去的地方至。 –
我一直想學習更新的版本,但有這個機器視覺課程,我花了這個學期,需要Python 2.7編程... – Arman