品易云推流 关闭
文章详情页
文章 > Python常见问题 > python里input怎么解释

python里input怎么解释

头像

silencement

2019-10-10 15:21:5112129浏览 · 0收藏 · 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学习网

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图