如何正确输出?

>>> g = input("enter number here:")
enter number here:43
>>> g + 65
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
g + 65
TypeError: Can't convert 'int' object to str implicitly
如何正确输出 g + 65

Helenr
浏览 95回答 2
2回答

料青山看我应如是

python 3.X input的输入默认是字符串,所以必须把 “g + 65”改为“int (g)+65”。Python3.x和Python2.x的区别1.性能&nbsp;Py3.0运行 pystone benchmark的速度比Py2.5慢30%。Guido认为Py3.0有极大的优化空间,在字符串和整形操作上可&nbsp;以取得很好的优化结果。&nbsp;Py3.1性能比Py2.5慢15%,还有很大的提升空间。&nbsp;2.编码&nbsp;Py3.X源码文件默认使用utf-8编码,这就使得以下代码是合法的:&nbsp;>>> 中国 = 'china'&nbsp;>>>print(中国)&nbsp;china&nbsp;3. 语法&nbsp;1)去除了<>,全部改用!=&nbsp;2)去除``,全部改用repr()&nbsp;3)关键词加入as 和with,还有True,False,None&nbsp;4)整型除法返回浮点数,要得到整型结果,请使用//&nbsp;5)加入nonlocal语句。使用noclocal x可以直接指派外围(非全局)变量&nbsp;6)去除print语句,加入print()函数实现相同的功能。同样的还有 exec语句,已经改为exec()函数&nbsp;例如:&nbsp;2.X: print "The answer is", 2*2&nbsp;3.X: print("The answer is", 2*2)&nbsp;2.X: print x, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# 使用逗号结尾禁止换行&nbsp;3.X: print(x, end=" ") &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # 使用空格代替换行&nbsp;2.X: print &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # 输出新行&nbsp;3.X: print() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # 输出新行&nbsp;2.X: print >>sys.stderr, "fatal error"&nbsp;3.X: print("fatal error", file=sys.stderr)&nbsp;2.X: print (x, y) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# 输出repr((x, y))&nbsp;3.X: print((x, y)) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # 不同于print(x, y)!&nbsp;7)改变了顺序操作符的行为,例如x<y,当x和y类型不匹配时抛出TypeError而不是返回随即的 bool值 &nbsp;8)输入函数改变了,删除了raw_input,用input代替:&nbsp;2.X:guess = int(raw_input('Enter an integer : ')) # 读取键盘输入的方法&nbsp;3.X:guess = int(input('Enter an integer : '))9)去除元组参数解包。不能def(a, (b, c)):pass这样定义函数了&nbsp;10)新式的8进制字变量,相应地修改了oct()函数。&nbsp;2.X的方式如下:&nbsp;>>> 0666&nbsp;438&nbsp;>>> oct(438)&nbsp;'0666'&nbsp;3.X这样:&nbsp;>>> 0666&nbsp;SyntaxError: invalid token (<pyshell#63>, line 1)&nbsp;>>> 0o666&nbsp;438&nbsp;>>> oct(438)&nbsp;'0o666'&nbsp;11)增加了 2进制字面量和bin()函数&nbsp;>>> bin(438)&nbsp;'0b110110110'&nbsp;>>> _438 = '0b110110110'&nbsp;>>> _438&nbsp;'0b110110110'&nbsp;12)扩展的可迭代解包。在Py3.X 里,a, b, *rest = seq和 *rest, a = seq都是合法的,只要求两点:rest是list&nbsp;对象和seq是可迭代的。&nbsp;13)新的super(),可以不再给super()传参数,&nbsp;>>> class C(object):&nbsp;def __init__(self, a):&nbsp;print('C', a)&nbsp;>>> class D(C):&nbsp;def __init(self, a):&nbsp;super().__init__(a) # 无参数调用super()&nbsp;>>> D(8)&nbsp;C 8&nbsp;<__main__.D object at 0x00D7ED90>&nbsp;14)新的metaclass语法:&nbsp;class Foo(*bases, **kwds):&nbsp;pass&nbsp;15)支持class decorator。用法与函数decorator一样:&nbsp;>>> def foo(cls_a):&nbsp;def print_func(self):&nbsp;print('Hello, world!')&nbsp;cls_a.print = print_func&nbsp;return cls_a&nbsp;>>> @foo&nbsp;class C(object):&nbsp;pass&nbsp;>>> C().print()&nbsp;Hello, world!&nbsp;class decorator可以用来玩玩狸猫换太子的大把戏。更多请参阅PEP 3129&nbsp;4. 字符串和字节串&nbsp;1)现在字符串只有str一种类型,但它跟2.x版本的unicode几乎一样。2)关于字节串,请参阅“数据类型”的第2条目&nbsp;5.数据类型&nbsp;1)Py3.X去除了long类型,现在只有一种整型——int,但它的行为就像2.X版本的long&nbsp;2)新增了bytes类型,对应于2.X版本的八位串,定义一个bytes字面量的方法如下:&nbsp;>>> b = b'china'&nbsp;>>> type(b)&nbsp;<type 'bytes'>&nbsp;str对象和bytes对象可以使用.encode() (str -> bytes) or .decode() (bytes -> str)方法相互转化。&nbsp;>>> s = b.decode()&nbsp;>>> s&nbsp;'china'&nbsp;>>> b1 = s.encode()&nbsp;>>> b1&nbsp;b'china'&nbsp;3)dict的.keys()、.items 和.values()方法返回迭代器,而之前的iterkeys()等函数都被废弃。同时去掉的还有&nbsp;dict.has_key(),用 in替代它吧&nbsp;6.面向对象&nbsp;1)引入抽象基类(Abstraact Base Classes,ABCs)。&nbsp;2)容器类和迭代器类被ABCs化,所以cellections模块里的类型比Py2.5多了很多。&nbsp;>>> import collections&nbsp;>>> print('\n'.join(dir(collections)))&nbsp;Callable&nbsp;Container&nbsp;Hashable&nbsp;ItemsView&nbsp;Iterable&nbsp;Iterator&nbsp;KeysView&nbsp;Mapping&nbsp;MappingView&nbsp;MutableMapping&nbsp;MutableSequence&nbsp;MutableSet&nbsp;NamedTuple&nbsp;Sequence&nbsp;Set&nbsp;Sized&nbsp;ValuesView&nbsp;__all__&nbsp;__builtins__&nbsp;__doc__&nbsp;__file__&nbsp;__name__&nbsp;_abcoll&nbsp;_itemgetter&nbsp;_sys&nbsp;defaultdict&nbsp;deque&nbsp;另外,数值类型也被ABCs化。关于这两点,请参阅 PEP 3119和PEP 3141。&nbsp;3)迭代器的next()方法改名为__next__(),并增加内置函数next(),用以调用迭代器的__next__()方法&nbsp;4)增加了@abstractmethod和 @abstractproperty两个 decorator,编写抽象方法(属性)更加方便。&nbsp;7.异常&nbsp;1)所以异常都从 BaseException继承,并删除了StardardError&nbsp;2)去除了异常类的序列行为和.message属性&nbsp;3)用 raise Exception(args)代替 raise Exception, args语法&nbsp;4)捕获异常的语法改变,引入了as关键字来标识异常实例,在Py2.5中:&nbsp;>>> try:&nbsp;... &nbsp; &nbsp;raise NotImplementedError('Error')&nbsp;... except NotImplementedError, error:... &nbsp; &nbsp;print error.message&nbsp;...&nbsp;Error&nbsp;在Py3.0中:&nbsp;>>> try:&nbsp;raise NotImplementedError('Error')&nbsp;except NotImplementedError as error: #注意这个 as&nbsp;print(str(error))&nbsp;Error&nbsp;5)异常链,因为__context__在3.0a1版本中没有实现&nbsp;8.模块变动&nbsp;1)移除了cPickle模块,可以使用pickle模块代替。最终我们将会有一个透明高效的模块。&nbsp;2)移除了imageop模块&nbsp;3)移除了 audiodev, Bastion, bsddb185, exceptions, linuxaudiodev, md5, MimeWriter, mimify, popen2, &nbsp;rexec, sets, sha, stringold, strop, sunaudiodev, timing和xmllib模块&nbsp;4)移除了bsddb模块5)移除了new模块&nbsp;6)os.tmpnam()和os.tmpfile()函数被移动到tmpfile模块下&nbsp;7)tokenize模块现在使用bytes工作。主要的入口点不再是generate_tokens,而是 tokenize.tokenize()&nbsp;9.其它&nbsp;1)xrange() 改名为range(),要想使用range()获得一个list,必须显式调用:&nbsp;>>> list(range(10))&nbsp;[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&nbsp;2)bytes对象不能hash,也不支持 b.lower()、b.strip()和b.split()方法,但对于后两者可以使用 b.strip(b’ &nbsp;\n\t\r \f’)和b.split(b’ ‘)来达到相同目的&nbsp;3)zip()、map()和filter()都返回迭代器。而apply()、 callable()、coerce()、 execfile()、reduce()和reload&nbsp;()函数都被去除了现在可以使用hasattr()来替换 callable(). hasattr()的语法如:hasattr(string, '__name__')4)string.letters和相关的.lowercase和.uppercase被去除,请改用string.ascii_letters 等&nbsp;5)如果x < y的不能比较,抛出TypeError异常。2.x版本是返回伪随机布尔值的&nbsp;6)__getslice__系列成员被废弃。a[i:j]根据上下文转换为a.__getitem__(slice(I, j))或 __setitem__和&nbsp;__delitem__调用&nbsp;7)file类被废弃,在Py2.5中:&nbsp;>>> file&nbsp;<type 'file'>&nbsp;在Py3.X中:&nbsp;>>> file&nbsp;Traceback (most recent call last):&nbsp;File "<pyshell#120>", line 1, in <module>&nbsp;file&nbsp;NameError: name 'file' is not defined&nbsp;

狐的传说

python 3.X input 输入默认是字符串改为int (g)+65
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python