Python3.0改成函数,一些问题新手求指教
看的教程是2.0的 ,3.0下不能运行,是不是3.0 input函数默认输入值以字符串形式存储还是什么意思?
ActivePython 2.7.2.5 (ActiveState Software Inc.) based on
Python 2.7.2 (default, Jun 24 2011, 12:22:14) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> x=input ()
21
>>> y=input ()
22
>>> print x*y
462
>>>
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:57:17) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> x=input ()
21
>>> y=input ()
22
>>> print x*y
SyntaxError: invalid syntax
>>> print(x*y),
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
print(x*y),
TypeError: can't multiply sequence by non-int of type 'str'
>>>
[解决办法]
恩,好像是要强转的
[解决办法]
input()进去的值是str类型的。
python虽然是动态类型的,但它也是强类型的,所以要显式地把str转成int
>>> x = input()21>>> y = input()11>>> x'21'>>> y'11'>>> print(x*y)Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: can't multiply sequence by non-int of type 'str'>>> print(int(x)*int(y))231>>>
[解决办法]
3.0将根据LANG编码将input输入内容专向unicode(即python3.x里的str对象)存储。