ITPub博客

首页 > IT职业 > IT职场 > python里input怎么解释

python里input怎么解释

IT职场 作者:Lexus 时间:2021-09-11 10:30:38 0 删除 编辑

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 "<stdin>", line 1, in <module>
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学习网。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3104/viewspace-2836099/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-30

  • 博文量
    86
  • 访问量
    612