python简单错误处理

来源:互联网 发布:pdf转图片软件 编辑:程序博客网 时间:2024/04/29 15:22

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
     >>> 

0 0