发布时间:2022-10-14 09:39:53 人气:96 作者:多测师
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培训之input怎么解释,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注多测师。https://www.e70w.com/xwzx/