如何将变量乘以变量?

时间:2018-10-19 05:06:22

标签: python-3.x

weight = print(int(input('weight: ')))
height = print (float(input('height: ')))

BMI = weight * height
print(BMI)

#i get this back
Traceback (most recent call last):
  File "C:/Users/Nicholas/Desktop/csp 17/Assign 3-2.py", line 4, in <module>
    BMI = weight * height
TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType'

1 个答案:

答案 0 :(得分:3)

print就是这样做的:它将内容打印到屏幕上。由于此函数不返回任何其他内容,因此将隐式返回None

如果需要,您可以通过列表理解来验证这一点:

>>> x = [print(i) for i in range(5)]
0
1
2
3
4
>>> x
[None, None, None, None, None]

请注意,所有内容均已打印,但结果变量是一个充满None的列表。

对于您的代码,请尝试不使用print,因为您要执行的操作不是打印这些内容,而是将变量weightheight分配给输入的值:

weight = int(input('weight: '))
height = float(input('height: '))

BMI = weight * height
print(BMI)
相关问题