Home > Back-end >  Restart Python script at specific point
Restart Python script at specific point

Time:02-02

Ok so I am very new to Python and I recently took on the challenge of creating a very simple calculator. It works completely fine but I want to add a option to restart and go back to the main menu area rather than restarting the program entirely. How could this be done?

here is the code that i have so far

from math import sqrt
import time


print("Welcome to Calculator")
print(" #1 Add \n #2 Subtract \n #3 Multiply \n #4 Divide \n #5 Square Root")
option = input("Select one #: ")

if option == "1":
    print("Addition:")
    num1 = input("Enter first Number: ")
    num2 = input("Enter Second Number: ")
    result = float(num1)   float(num2)
    print("The answer is: ", result)


elif option == "2":
    print("Subtraction:")
    num1 = input("Enter first Number: ")
    num2 = input("Enter Second Number: ")
    result = float(num1) - float(num2)
    print("The answer is: ", result)

elif option == "3":
    print("Multiplication:")
    num1 = input("Enter first Number: ")
    num2 = input("Enter Second Number: ")
    result = float(num1) * float(num2)
    print("The answer is: ", result)

elif option == "4":
    print("Division:")
    num1 = input("Enter first Number: ")
    num2 = input("Enter Second Number: ")
    result = float(num1) / float(num2)
    print("The answer is: ", result)

elif option == "5":
    print("Square Root:")
    num1 = input("Enter Number: ")
    result = sqrt(float(num1))
    print("The answer is: ", result)

I tried the adding While True to the program but I was not able to figure out how that works.

CodePudding user response:

Wrapping your whole code in a while True loop would work but you need to write an if statement at the end in order to exit the infinite loop. Here's the working code:

from math import sqrt
import time

while True:
    print("Welcome to Calculator")
    print(" #1 Add \n #2 Subtract \n #3 Multiply \n #4 Divide \n #5 Square Root")
    option = input("Select one #: ")

    if option == "1":
        print("Addition:")
        num1 = input("Enter first Number: ")
        num2 = input("Enter Second Number: ")
        result = float(num1)   float(num2)
        print("The answer is: ", result)

    elif option == "2":
        print("Subtraction:")
        num1 = input("Enter first Number: ")
        num2 = input("Enter Second Number: ")
        result = float(num1) - float(num2)
        print("The answer is: ", result)

    elif option == "3":
        print("Multiplication:")
        num1 = input("Enter first Number: ")
        num2 = input("Enter Second Number: ")
        result = float(num1) * float(num2)
        print("The answer is: ", result)

    elif option == "4":
        print("Division:")
        num1 = input("Enter first Number: ")
        num2 = input("Enter Second Number: ")
        result = float(num1) / float(num2)
        print("The answer is: ", result)

    elif option == "5":
        print("Square Root:")
        num1 = input("Enter Number: ")
        result = sqrt(float(num1))
        print("The answer is: ", result)

    restart = input("Do you want to restart the program? (yes/no) ")
    if restart.lower() != "yes":
        break

CodePudding user response:

You can add while loop like this:

while True:
   option = input("Select one #: ")

   if option == "1":
       print("Addition:")
   ...

   elif option == "5":
       print("Square Root:")
       num1 = input("Enter Number: ")
       result = sqrt(float(num1))
       print("The answer is: ", result)
  • Related