成都网站建设设计

将想法与焦点和您一起共享

创新互联Python教程:python里input怎么解释

python中input函数有类似c中的scanf函数的功能。

Python2中input使用如下:

>>>x = input("x:")
x: 3
>>>y = input("y:" )
y: 4
>>> print x*y
12

但是Python3中input使用会有如下的提示:

>>> x = input("x:")
x:3
>>> y = input("y:")
y:4
>>> print (x*y)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't multiply sequence by non-int of type 'str'
>>>

原因:
Python3以后的版本中,raw_input和input合体了,取消了raw_input,并用input代替,所以说现在版本的input接受的是字符串,可以如下处理:

>>> x = int(input("x:"))
x:3
>>> y = int(input("y:"))
y:4
>>> print (x*y)
12

更多学习内容,请点击Python学习网。


分享文章:创新互联Python教程:python里input怎么解释
网页URL:https://chengdu.cdxwcx.cn/article/djchpps.html