#Finding the volume of a box
print("Welcome to box volume calculation! Please answer the following questions.")
x = float(raw_input("How wide is the box? ")),
y = float(raw_input("How high is the box? ")),
z = float(raw_input("How long is the box? ")),
"The volume of the box is " + str(x*y*z) + " units cubed."
我收到的错误消息:
Traceback (most recent call last):
File "C:\Python25\Scripts\Randomness.py", line 22, in <module>
"The volume of the box is " + str(x*y*z) + " units cubed."
TypeError: can't multiply sequence by non-int of type 'tuple'
答案 0 :(得分:3)
在您要求输入的行中删除逗号。这些行应为:
x = float(raw_input("How wide is the box? "))
y = float(raw_input("How high is the box? "))
z = float(raw_input("How long is the box? "))
解释......表格声明:
x = a, b, c
创建一个由三个元素组成的元组,同样地:
x = a,
创建一个一个元素的元组。所以在这里,声明如下:
x = float(raw_input(...)),
创建一个元素的元组,该元素是您的输入!
答案 1 :(得分:1)
你的变量是元组而不是浮点数:
x = float(raw_input("How wide is the box? ")),
# ^
尾随逗号使您的对象成为包含一个浮点数的元组:
>>> x = 2.2,
>>> type(x)
<class 'tuple'>
怎么办?删除所有尾随逗号:
x = float(raw_input("How wide is the box? "))
额外:错误与您不想要的内容有关,解释器正确地假设您正在尝试使用另一个元组扩展元组。但是,元组只能使用整数扩展:
>>> x = 2.2,
>>> x * 5
(2.2, 2.2, 2.2, 2.2, 2.2)