Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python错误;+;的操作数类型不受支持:';int';和';str';_Python_Python 2.7_Python 2.x - Fatal编程技术网

python错误;+;的操作数类型不受支持:';int';和';str';

python错误;+;的操作数类型不受支持:';int';和';str';,python,python-2.7,python-2.x,Python,Python 2.7,Python 2.x,我是python新手,在编写这段代码的过程中,第8行不断出现错误,说“不支持+:'int'和'str'的操作数类型”,但我不知道如何修复它/它有什么问题 minTot = 0 stepTot = 0 min = int(raw_input("Input the number of minutes (0 to exit): ")) if min == 0: print "No minutes input." else: while min != 0: minTot

我是python新手,在编写这段代码的过程中,第8行不断出现错误,说“不支持+:'int'和'str'的操作数类型”,但我不知道如何修复它/它有什么问题

minTot = 0
stepTot = 0
min = int(raw_input("Input the number of minutes (0 to exit): "))
if min == 0:
    print "No minutes input."
else:
    while min != 0:
        minTot = minTot + min
        stepRate = int(raw_input("Input the step rate: "))
        stepTot = stepTot + stepRate * min
        min = raw_input("Input the next number of minutes (0 to exit): ")
    print "\nTotal number of minutes:", min
    print "Total number of steps:", stepTot
    # Average is rounded down.
print " Average step rate per minute : ", minTot / stepTot

我相信您正在使用Python2.7。(您可以使用
python--version
)确认这一点,问题就在这一行

min = raw_input("Input the next number of minutes (0 to exit): ")
raw\u input
返回一个字符串,您需要使用
int
将其显式转换为一个数字,如下所示

min = int(raw_input("Input the next number of minutes (0 to exit): "))
如果不这样做,
min
将是一个字符串,在下一次迭代中,当它达到

minTot = minTot + min
minTot
将是一个数字,您正在尝试添加带有数字的字符串。这是不可能的。这就是Python抛出该错误的原因


除此之外,还有一个内置函数的名称。您可能不想隐藏该函数。因此,请使用其他变量名。

您确定使用的是Python 3.x吗?因为它没有
raw\u input
函数。第11行缺少
int()
调用,这是python 2
min
是一个,所以您可能应该选择另一个名称。我强烈建议您研究铸造,基本思想是str(您的变量)。这将把变量转换为字符串。您可以在:和:处阅读更多内容。注意,您还可以将字符串强制转换为整数、整数强制转换为字符串、对象等等。