2016-07-04 5 views
0
#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." 

エラーメッセージ私は取得しています:あなたが入力を求めているところ"TypeError:型 'tuple'の非整数によるシーケンスの乗算はできません。

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

は、なぜあなたはコンマを末尾にしているのですか?チュートリアルや他のリソースがこれを示唆しているのは何ですか? – TigerhawkT3

答えて

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 

は、3つの要素のタプルを作成し、同じように:

x = a, 

はのタプルを作成し、 要素。したがって、ここには次のようなステートメントがあります。

x = float(raw_input(...)), 

入力はその要素の1組のタプルを作成します。

1

あなた変数は山車タプルではありません。

x = float(raw_input("How wide is the box? ")), 
#           ^

末尾のコンマは、オブジェクト1台のフロートを含むタプルを行います

>>> 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) 
関連する問題