2017-10-03 12 views
1

私はユーザーが値(1,2,3)を入力できるスクリプトを作成しようとしています。答えを見つけるために使うことができます。しかし、私は、ユーザが数学関数を再起動するか、メニューに戻って別の関数を選択するかを尋ねるかどうかを尋ねたい。Python 3.0+:IF文を使用してコードの一部を再起動しようとしています

私は数週間Pythonを勉強してきただけなので、どのようにすればいいのか分かりません。ありがとう!ここで

は私のコードです:

choice = int(input("What program would you like to run? \n")) 

#chooses one of the programs 
recarea = 1 
circarea = 2 
fah = 3 

if choice == 1: 

    print("This program calculates the area and perimeter of a rectangle:") 

    length = float(input("Type in the length: \n")) 
    width = float(input("Type in the width: \n")) 
    area = length * width 
    perimeter = (2*length)+(2*width) 

    print("The area of the rectangle is ","{0:.2f}".format(area),"cm and the perimeter is ","{0:.2f}".format(perimeter),"cm",'\n\n') 

    reuse = str(input("Would you like to restart the script ?")) 

    if reuse == "Yes" or reuse == "yes" or reuse == "y" or reuse == "Y": 
     print("Restarting Script...\n") 

    else: 
     reuse == "No" or reuse == "no" or reuse == "n" or reuse == "N" 
     print("Returning to the menu...\n\n") 
+0

ようこそStackOverflow! [機能](https://www.tutorialspoint.com/python/python_functions.htm)をお読みになることをお勧めします。 1つの機能があれば、その機能を呼び出して再起動することができます。 –

+1

ループを使用する必要があります。おそらくwhileループが最適でしょう。しかし、それはあまりにも広い質問です。ループをルックアップし、前述のように、関数。 – Carcigenicate

答えて

0

あなたがアップ機能にコードを分割する必要があります。

def main(): 
    print("What program would you like to run? \n") 
    choice = int(input("1 - rectangle area\n2 - circle area\n3 - fah")) 
    if choice == 1: 
     print("This program calculates the area and perimeter of a rectangle:") 
     length = float(input("Type in the length: \n")) 
     width = float(input("Type in the width: \n")) 
     area = length * width 
     perimeter = (2*length)+(2*width) 
     print("The area of the rectangle is ","{0:.2f}".format(area),"cm and the perimeter is ","{0:.2f}".format(perimeter),"cm",'\n\n') 
    elif choice == 2: 
     print("Not implemented.") 
    elif choice == 3: 
     print("Not implemented.") 
    else: 
     print("I didn't understand that input please type 1, 2 or 3.") 
     main() 
    askRestart() 

def askRestart(): 
    reuse = str(input("Type 'Y' to restart or 'X' to exit().")) 
    if reuse.lower() == 'y': 
     main() 
    elif reuse.lower() == 'x': 
     pass 
    else: 
     print("I didn't understand that input please type 'Y' or 'X'") 
     askRestart() 

その後、プログラムを実行する関数を呼び出します。

main() 
0

あなたは可能性がありwhile True:ブロック内にコードをラップするだけです。これにより、セグメント全体が無限に繰り返されます。

while True: 
    choice = int(input("What program would you like to run? \n")) 

    #chooses one of the programs 
    recarea = 1 
    circarea = 2 
    fah = 3 

    if choice == 1: 

     print("This program calculates the area and perimeter of a rectangle:") 

     length = float(input("Type in the length: \n")) 
     width = float(input("Type in the width: \n")) 
     area = length * width 
     perimeter = (2*length)+(2*width) 

     print("The area of the rectangle is ","{0:.2f}".format(area),"cm and the perimeter is ","{0:.2f}".format(perimeter),"cm",'\n\n') 

     reuse = str(input("Would you like to restart the script ?")) 

     if reuse == "Yes" or reuse == "yes" or reuse == "y" or reuse == "Y": 
      print("Restarting Script...\n") 

     else: 
      reuse == "No" or reuse == "no" or reuse == "n" or reuse == "N" 
      print("Returning to the menu...\n\n") 

もう1つのオプションは、特定の値が入力された場合にループを終了することです。たとえば、0が入力された場合、このバージョンは終了します。

choice=None 
while choice != 0: 
    choice = int(input("What program would you like to run? (enter 0 to exit) \n")) 

    #chooses one of the programs 
    recarea = 1 
    circarea = 2 
    fah = 3 

    if choice == 1: 

     print("This program calculates the area and perimeter of a rectangle:") 

     length = float(input("Type in the length: \n")) 
     width = float(input("Type in the width: \n")) 
     area = length * width 
     perimeter = (2*length)+(2*width) 

     print("The area of the rectangle is ","{0:.2f}".format(area),"cm and the perimeter is ","{0:.2f}".format(perimeter),"cm",'\n\n') 

     reuse = str(input("Would you like to restart the script ?")) 

     if reuse == "Yes" or reuse == "yes" or reuse == "y" or reuse == "Y": 
      print("Restarting Script...\n") 

     else: 
      reuse == "No" or reuse == "no" or reuse == "n" or reuse == "N" 
      print("Returning to the menu...\n\n") 
関連する問題