2016-05-26 5 views
-2
# This is a calcualtor 

shape = raw_input('what shape do you want to calculate? rectangle, circle, triangle, square? ') 

if shape == 'rectangle': 

    width = raw_input('Please tell me the width of rectangle: ') 
    height = raw_input('Please tell me the height of rectangle: ') 
    print 'the area of the rectange is: ', width*height 

elif shape == 'circle': 
    radius = raw_input('Please tell me the radius of the circle: ') 
    print 'the area of the circle is: ', 3.14 * int(radius)**2 
+2

サンプル入力、誤った出力、目的の出力でコードを編集するのではなく、 'width'と' height'を 'int'にキャストしていないように見えます。 'width = int(raw_input( '矩形の幅を教えてください'))' – EdChum

+0

'rectangle'では' width'と 'height'を整数に変換しません。また、両方の 'if'のprint文が間違っている場合は、文字列に数値を掛けているようです。 'print 'を試してみてください。%d'%(3.14 * int(radius)** 2)' https://pyformat.info/ – fips

答えて

2

raw_inputは文字列を返します。 width = int(raw_input(...))を使用し、同じものをheightとして実際の整数に変換してください。デモ:

>>> width = raw_input('Please tell me the width of rectangle: ') 
Please tell me the width of rectangle: 5 
>>> width 
'5' 
>>> type(width) 
<type 'str'> 
>>> width * width 
Traceback (most recent call last): 
    File "<input>", line 1, in <module> 
TypeError: cannot multiply sequence by non-int of type 'str' 
>>> width_int = int(width) 
>>> width_int 
5 
>>> type(width_int) 
<type 'int'> 
>>> width_int * width_int 
25 

利用float代わりのintあなたは小数部を持つ数字をしたい場合。

0

width & heightがstrであるため、can't multiply sequence of non-int of type 'str'が得られます。

width = int(raw_input('Please tell me the width of rectangle: ')) 

使用のPython 3.xので:

width = int(input('Please tell me the width of rectangle: ')) 

デフォルトinputするか、文字列を返すraw_input

使用のpython 2.xので。

小数点/浮動小数点数の代わりにint()の代わりにfloat()を使用します。

また、あなたは、単一の行に入力の両方を取ることがあります。

side = map(int, raw_input().split()) 

多分上記のケースではあなたの入力:あなたはwidth & heightを含むリストsideを取得

そして、整数として。

+0

thxを参考にしてください。 – lookyourphone

関連する問題