2016-10-26 9 views
0

私は、入力によって決定される形状の体積を決定するコードを持っています。私はいくつかのif文をwhileループにネストしています。私はループが入力を求める場所の始めから再開したいのですが、代わりに条件文に詰まっています。たとえば、「キューブ」と入力すると、ボリュームが計算され、次に寸法を入力するように求められます。代わりに、形状を尋ねて、if文をもう一度実行します。最終的に同じですが、有効でない図形を入力すると、もう一度入力するように求められ、その入力が有効であればそれに応じて処理を進めます。しかし、それは無限ループに詰まらず、形を入力するように求めることが決して止まらない。Python - whileループ内のif文は、条件ではなくループを再起動しますか?

import math 
import decimal 

shape = str(input("Please enter a shape: ")) 
shape = shape.lower() 

shapes = ["cube", "pyramid", "ellipsoid", "quit"] 

stored_cube_vol = [] 
stored_pymd_vol = [] 
stored_ellip_vol = [] 

while shape != "quit": 
    if shape == "cube": 
     def cube_volume(cube_side_length): 
      cube_total_vol = decimal.Decimal(cube_side_length**3) 
      return round(cube_total_vol, 2) 

     cube_side_length = float(input("Enter the length of length of one side of the cube: ")) 

     print("The volume of a cube with sides with a length of", cube_side_length, "is", cube_volume(cube_side_length)) 

     stored_cube_vol.append(cube_volume(cube_side_length)) 

    elif shape == "pyramid": 
     def pymd_volume(pymd_base, pymd_height): 
      pymd_total_vol = decimal.Decimal(pymd_base**2)*pymd_height*(1/3) 
      return round(pymd_total_vol, 2) 

     pymd_base = float(input("Enter the length of the base of the pyramid: ")) 
     pymd_height = float(input("Enter the height of the pyramid: ")) 

     print("The volume of a pyramid with a base of", pymd_base, "and a height of", pymd_height, "is", pymd_volume(pymd_base, pymd_height)) 

     stored_pymd_vol.append(pymd_volume(pymd_base, pymd_height)) 

    elif shape == "ellipsoid": 
     def ellip_volume(ellip_rad_1, ellip_rad_2, ellip_rad_3): 
      ellip_total_vol = decimal.Decimal(math.pi*(4/3)*ellip_rad_1*ellip_rad_2*ellip_rad_3) 
      return round(ellip_total_vol, 2) 

     ellip_rad_1 = float(input("Enter the first radius: ")) 
     ellip_rad_2 = float(input("Enter the second radius: ")) 
     ellip_rad_3 = float(input("Enter the third radius: ")) 

     print("The volume of an ellipsoid with radii", ellip_rad_1, ellip_rad_2, "and", ellip_rad_3, "is", ellip_volume(ellip_rad_1, ellip_rad_2, ellip_rad_3)) 

     stored_ellip_vol.append(ellip_volume(ellip_rad_1, ellip_rad_2, ellip_rad_3)) 

    elif shape == "quit": 
     print("You have come to the end of the session.") 
     print("The volumes calculated for each shape are shown below.") 
     print("Cube: ", sorted(stored_cube_vol)) 
     print("Pyramid: ", sorted(stored_pymd_vol)) 
     print("Ellipsoid: ", sorted(stored_ellip_vol)) 

    else: 
     str(input("Please enter a shape: ").lower()) 
+0

なぜあなたはループの内部で関数を定義していますか? –

+0

私は何をしているのかわからないので... whileループが始まる前にそれらのすべてを置くことができますか? – DR2016

+0

whileループの開始時にshape = input ...を呼び出すとします。キューブ計算後の時点では、シェイプ=キューブはまだループ内に戻ります。 – Chris

答えて

0

あなたelse句でshape =を逃しています。

私が正しく理解していれば、単にelse句を削除し、残して:

shape = str(input("Please enter a shape: ")).lower() 

ループの終わり。

有効な選択肢を入力したとしても、"quit"と入力しない限り、このメッセージが表示されます。

+0

これはありがとうございました!しかし、今私は問題を抱えています。私は終了したら、それが何をするのかを印刷しません。プロセスはちょうど終了します。 – DR2016

+0

while条件を単に 'while true'に変更してから、あなたの終了条件 – Chris

0

私はコードをいくつか変更しました。ユーザーからの入力をwhileループに入れて、 'if ==' quit ''ステートメントにbreakステートメントを追加しました。

変更1:

import math 
import decimal 

shapes = ["cube", "pyramid", "ellipsoid", "quit"] 

stored_cube_vol = [] 
stored_pymd_vol = [] 
stored_ellip_vol = [] 
shape = " " 
while shape != "quit": 
    shape = str(input("Please enter a shape: ")) 
    shape = shape.lower() 

が変更2:

elif shape == "quit": 
    print("You have come to the end of the session.") 
    print("The volumes calculated for each shape are shown below.") 
    print("Cube: ", sorted(stored_cube_vol)) 
    print("Pyramid: ", sorted(stored_pymd_vol)) 
    print("Ellipsoid: ", sorted(stored_ellip_vol)) 
    break 
関連する問題