2017-11-10 9 views
0

私はクラスのための簡単なプログラムを書いた。ユーザは、解決する5つの異なる質問、円の面積、円柱/立方体の体積、または円柱/立方体の表面積の選択肢をユーザに与える。ユーザーは解決したい問題を決定しなければならず、無効な決定を下した場合、プログラムは最初にループして再試行されます。しかし、私はループを壊す方法を理解することはできません。問題の1つを解決した後も、プログラムの開始に戻ります。Pythonでこのループを解く方法

invalid_input = True 

def start() : 

    #Intro 
    print("Welcome! This program can solve 5 different problems for you.") 
    print() 
    print("1. Volume of a cylinder") 
    print("2. Surface Area of a cylinder") 
    print("3. Volume of a cube") 
    print("4. Surface Area of a cube") 
    print("5. Area of a circle") 
    print() 


    #Get choice from user 
    choice = input("Which problem do you want to solve?") 
    print() 

    if choice == "1": 

      #Intro: 
      print("The program will now calculate the volume of your cylinder.") 
      print() 

      #Get radius and height 

      radius = float(input("What is the radius?")) 
      print() 
      height = float(input("What is the height?")) 
      print() 

      #Calculate volume 
      if radius > 0 and height > 0: 
       import math 

       volume = math.pi * (radius**2) * height 
       roundedVolume = round(volume,2) 

      #Print volume 
       print("The volume is " + str(roundedVolume) + (" units.")) 
       invalid_input = False 

      else: 
       print("Invalid Inputs, please try again.") 
       print() 


    elif choice == "2": 

      #Intro: 
      print("The program will calculate the surface area of your cylinder.") 
      print() 

      #Get radius and height 

      radius = float(input("What is the radius?")) 
      print() 
      height = float(input("What is the height?")) 
      print() 

      #Calculate surface area 
      if radius > 0 and height > 0: 
       import math 
       pi = math.pi 
       surfaceArea = (2*pi*radius*height) + (2*pi*radius**2) 
       roundedSA = round(surfaceArea,2) 

      #Print volume 
       print("The surface area is " + str(roundedSA) + " units.") 
       invalid_input = False 

      elif radius < 0 or height < 0: 
       print("Invalid Inputs, please try again.") 
       print() 


    elif choice == "3": 

      #Intro: 
      print("The program will calculate the volume of your cube.") 
      print() 

      #Get edge length 

      edge = float(input("What is the length of the edge?")) 
      print() 

      #Calculate volume 
      if edge > 0: 

       volume = edge**3 
       roundedVolume = round(volume,2) 

       #Print volume 
       print("The volume is " + str(roundedVolume) + (" units.")) 
       invalid_input = False 

      else: 
       print("Invalid Edge, please try again") 
       print() 


    elif choice == "4": 

      #Intro: 
      print("The program will calculate the surface area of your cube.") 
      print() 

      #Get length of the edge 

      edge = float(input("What is the length of the edge?")) 
      print() 


      #Calculate surface area 
      if edge > 0: 

       surfaceArea = 6*(edge**2) 
       roundedSA = round(surfaceArea,2) 

      #Print volume 
       print("The surface area is " + str(roundedSA) + (" units.")) 
       invalid_input = False 

      else: 
        print("Invalid Edge, please try again") 
        print() 



    elif choice == "5": 

      #Intro 
      print("The program will calculate the area of your circle") 
      print() 

      #Get radius 
      radius = float(input("What is your radius?")) 

      if radius > 0: 

      #Calculate Area 
       import math 
       area = math.pi*(radius**2) 
       roundedArea = round(area,2) 
       print("The area of your circle is " + str(roundedArea) + " units.") 
       invalid_input = False 

      else: 
       print("Invalid Radius, please try again") 
       print() 


    else: 
     print("Invalid Input, please try again.") 
     print() 

while invalid_input : 
    start() 
+0

より良いあなたは有効な入力を持っていることを確認するための関数を書くには?どのような場合にプログラムを停止したいですか?何らかの理由で、invalid_inputはループをFalseとして終了しません。ブレークしたいところにブレークステートメントを追加するだけです –

答えて

1

この種のコードのための好ましい方法は、あなたが正しくneeds.Hereのために変更することができ、

while True: 
n = raw_input("Please enter 'hello':") 
if n.strip() == 'hello': 
    break 

これは一例であり、そのようwhile Trueステートメントを使用することであるlinkにありますドキュメンテーション。

0

終了オプションを追加できます。

print('6. Exit') 

... 
if choice=='6': 
    break 

有効でない入力を破棄することもできます。

else: 
    break 
0

invalid_input = Trueは、グローバル変数です(関数外)。

関数内のinvalid_input = Falseを設定すると、グローバル変数とは無関係にローカル変数が設定されます。グローバル変数を変更するには、以下の(大幅に単純化された)コード必要があります。

invalid_input = True 

def start(): 
    global invalid_input 
    invalid_input = False 

while invalid_input: 
    start() 

をグローバル変数は、しかし、回避することが最善です。

def get_choice(): 
    while True: 
     try: 
      choice = int(input('Which option (1-5)? ')) 
      if 1 <= choice <= 5: 
       break 
      else: 
       print('Value must be 1-5.') 
     except ValueError: 
      print('Invalid input.') 
    return choice 

例:あなたが抜け出すしたい

>>> choice = get_choice() 
Which option (1-5)? one 
Invalid input. 
Which option (1-5)? 1st 
Invalid input. 
Which option (1-5)? 0 
Value must be 1-5. 
Which option (1-5)? 6 
Value must be 1-5. 
Which option (1-5)? 3 
>>> choice 
3 
関連する問題