2016-09-17 6 views
1

ちょうど2,3時間前にpythonを起動して問題を見つけました。 ブロースニペットは、最初にuserInputを1または2として保存し、その後、ifブロックを実行することを示しています。問題は、コードが(たとえ私がコンソールウィンドウに1を入力したとしても)他の部分にまっすぐジャンプすることです。 私は簡単な間違いをしていますが、どんな助けでも感謝します。 ===具体的にはCPython私のコードはIFブロックをスキップしてELSEに行く

userInput = float(input("Choose 1 (calculator) or 2 (area check)\n")) 
if(userInput =='1') : 
    shape = input("Please enter name of shape you would like to calculate the area for\n") 

if(shape == 'triangle') : 
    base = int(input("Please enter length of BASE\n")) 
    height = int(input("Please enter HEIGHT\n")) 
    print("The area of the triangle is %f" % ((base * height)*0.5)) 

elif (shape == 'circle') : 
    radius = int(input("Please enter RADIUS\n")) 
    print("The Area of the circle is %f" % ((radius**2)*22/7)) 

elif (shape == 'square') : 
    length = int(input("Please Enter LENGTH\n")) 
    print("The area of the square is %f" % ((length**2)*4)) 

else : 
    initial1 = float(input("Please enter a number\n")) 
    sign1 = input("please enter either +,-,*,/ \nwhen you wish to exit please type exit\n") 
    initial2 = float(input("Please enter number 2\n")) 
if(sign1 == '+') : 
    answer = float(initial1) + float(initial2) 
    print(answer) 
elif(sign1 == '*') : 
    answer = float(initial1) * float(initial2) 
    print(answer) 
elif(sign1 == '-') : 
    answer = float(initial1) - float(initial2) 
    print(answer) 
elif(sign1 == '/') : 
    answer = float(initial1)/float(initial2) 
    print(answer) 

PSのVisual Studio 2015で実行されているのPython 3.5を使用して

===。もし可能ならば、私は基本を完全に理解していることを確認したいので、できるだけ基本的な助けをとることができますか?

ありがとうございました! :D

+0

浮動小数点数に変換していますが、文字列と比較しています –

答えて

1

入力をfloatに変換していますが、数値の文字列を確認しています。それを変更します。

If userInput == 1.0: 

いっそ、それにそれは方法を維持し、ちょうど最初の場所で浮いているように、ユーザーの入力を変換しません。数値を計算したい場合は、入力をfloatまたはintに変換する必要があります。あなたの場合は、オプションとしてそのまま使用しているので、文字列としてそのまま使用できます。

userInput = input("Choose 1 (calculator) or 2 (area check)\n") 

P.S. Pythonのインデントには非常に注意してください。あなたのインデントがコードエディタで正しいと思っていますが、このサイトに貼り付ける際にも注意してください。ここではすべて同じレベルのものがありますが、あなたのプログラムが正しく動作するためには、あなたのifブロックのいくつかをさらにインデントする必要があります。

+0

ありがとうございます@elethan - 完璧に作業しました。それが意味をなさないので、「フロート」を取り除くのは辛いです。 –

関連する問題