python3.X中简单错误处理

时间:2016-06-21 17:40:49   收藏:0   阅读:1171

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

原文:http://www.cnblogs.com/nucdy/p/5604149.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!