2017-02-07 17 views
-4

これは単なる入力/出力コード とは何ですか: "Area"または "Perimeter"または "volume"を入力すると、特定の文字列を入力して計算します。このコードは長さをチェックし、入力に数字が含まれているかどうかをチェックし、長さを超えている場合はエラーを出力します。Pythonの入力に特定の単語が含まれていないか確認してください。

目的:「地域」、「境界線」または「ボリューム」が入力されている場合は、次の機能に進みます。..

print ("This program will find the area, perimeter and volume of the Rectangle!") 

    find = input ("Type in one to calculate "area", "perimeter" and volume": ") 

    if len(find) == 4 and find.isalpha(): #This is area, people can type 4 chars and can get away with it, is there a way to fix it? 
     w = int (input ("What is the Width of the rectangle: ")) 
     l = int (input ("What is the Length of the rectangle: ")) 
     a = w*l 
     ans = "The area of the rectangle is %s units!" 
     print (ans%(a)) 
    elif len (find) == 6 and find.isalpha(): # This is Volume 
     w = int(input ("What is the Width of the rectangle: ")) 
     l = int(input ("What is the Length of the rectangle: ")) 
     h = int(input ("What is the Height of the rectangle: ")) 
     v = l*w*h 
     ans = "The volume of the rectangle is %s units!" 
     print (ans%(v)) 
    elif len (find) == 9 and find.isalpha(): #This is Perimeter 
     w = int (input ("What is the Width of the rectangle: ")) 
     l = int (input ("What is the Length of the rectangle: ")) 
     p = 2*(l+w) 
     ans = "The primeter of the rectangle is %s units!" 
     print (ans%(p)) 
    else: 
     print ("You spelled area, perimeter or volume wrong, or what you typed in includes NUMBERS!") 
+3

いくつか問題があります。あなたの引用符を確認する '' 'もしあなたが今すぐ 'blah'と入力すれば、あなたのコードはそれが 'エリア'だと思うでしょう... find.lower()== 'area':' – depperm

+6

あなたの宿題の質問私たちは宿題の手伝いに反対しているわけではありませんが、あなたは自分の問題点を実際には教えていません。実際にはほとんどの問題があります。これは、あなたが望むものを達成するために、ほとんど完全に間違った方法です。 –

答えて

5

あなたは単に文字列の代わりに、それをチェックすることを比較することができます長さ
例:周囲長とボリュームの

if find == "area": #to check if the user input is "area" 

とそう..

+0

あなたは 'find.lower()'を望むでしょう。 – depperm

関連する問題