1. 程式人生 > >python簡單錯誤處理

python簡單錯誤處理

1.print

    >>> print 'hello world'
  SyntaxError: Missing parentheses in call to 'print'
   >>>

python版本更新後,3.X的版本中去掉了很多的函式,在3.X版本的python中,print需要加上括號

如:

    >>> print ('hello world')
    hello world
    >>> 

 另:將資料輸出為一組時,python2.x直接在需要輸出資料後面加上“,”即可,但python3.x中使用此方法無效,應該使用如下程式碼:

     >>>   print (item, end=" ")

2.input

   >>> myName=raw_input('Ener your name:')
   Traceback (most recent call last):
   File "<pyshell#129>", line 1, in <module>
   myName=raw_input('Ener your name:')
   NameError: name 'raw_input' is not defined
   >>> 

  同1,因版本問題。可直接用input代替

如:

     >>> myName=input('Ener your name:')
    Ener your name:cookie
    >>>

3.decimal

     >>> print (decimal.Decimal('1.1'))
    Traceback (most recent call last):
    File "C:/Users/cookie/Desktop/bb.py", line 2, in <module>
    print (decimal.Decimal('1.1'))
    NameError: name 'decimal' is not defined
    >>> 

錯誤提示‘decimal’ 未定義,匯入decimal包即可

如:

     >>> import decimal
     >>> print (decimal.Decimal('1.1'))
         1.1
     >>>