python里input怎么解释

317次阅读
没有评论

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 "<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学习网。

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:Python基础教程2022-12-07发表,共计519字。
新手QQ群:570568346,欢迎进群讨论 Python51学习