How to restart a program at a certain point in Python

26,535

Solution 1

The best way of doing this is probably with a while loop.

while True:
    ## your code
    if cont != "yes":
        break
## quit

Solution 2

Using a while loop, which keeps executing the block as long as the condition, cont == "yes", is true, i.e. it stops when the condition becomes false. After the while loop stops, the code after it is executed, in this case print("Bye, thanks for using the calculator.").

PS: The brackets around a and b in print ((a) + (b)) are unnecessary. Similarly, the brackets around opera and cont are also unnecessary. Also, the space after print makes it a little hard to see which function the arguments are part of. I'd suggest you remove the space. Otherwise for a beginner-level programmer the code is good. Once you become more experienced with Python, you might want to use a dictionary mapping the names of the operator into the functions in the operator module.

import time
print ("Welcome. This is a calculator that uses the function: A (operator) B.")
time.sleep(3.5)
print ("Available operators include: Addition, Subtraction, Multiplication, Division,        Exponent, and Remainder division.")
time.sleep(3.5)
cont = "yes" # So that the first time the while loop block will run
while cont == "yes":
    a = float(input("Type in a value of A. "))
    b = float(input("Type in a value of B. "))
    operb = input("Would you like to: Add - Subtract - Multiply - Divide - Exponent - or Remainder? ")
    opera = operb.lower()
    if (opera) == "add":
        print ((a) + (b))
    elif (opera) == "subtract":
        print ((a) - (b))
    elif (opera) == "multiply":
        print ((a) * (b))
    elif (opera) == "divide":
        print ((a) / (b))
    elif (opera) == "exponent":
        print ((a) ** (b))
    elif (opera) == "remainder":
        print ((a) % (b))
    else:
        print ("Invalid operation.")
    cont = input("Would you like to do another problem?")
    cont = cont.lower()

print("Bye, thanks for using the calculator.")

Solution 3

You most likely going to want to use a while loop, something like:

import time
print ("Welcome. This is a calculator that uses the function: A (operator) B.")
time.sleep(3.5)
print ("Available operators include: Addition, Subtraction, Multiplication, Division,        Exponent, and Remainder division.")
time.sleep(3.5)
while True:
    a = float(input("Type in a value of A. "))
    if a == 'q':  # set up a condition to end the program
        return
Share:
26,535
user2975375
Author by

user2975375

Updated on February 02, 2020

Comments

  • user2975375
    user2975375 over 4 years

    So I made a very primitive and probably inefficient calculator today (first time using Python), and I want to be able to continue doing more problems, how would I do so? Here is my "calculator" app..

    import time
    print ("Welcome. This is a calculator that uses the function: A (operator) B.")
    time.sleep(3.5)
    print ("Available operators include: Addition, Subtraction, Multiplication, Division,        Exponent, and Remainder division.")
    time.sleep(3.5)
    a = float(input("Type in a value of A. "))
    b = float(input("Type in a value of B. "))
    operb = input("Would you like to: Add - Subtract - Multiply - Divide - Exponent - or Remainder? ")
    opera = operb.lower()
    if (opera) == "add":
        print ((a) + (b))
    elif (opera) == "subtract":
        print ((a) - (b))
    elif (opera) == "multiply":
        print ((a) * (b))
    elif (opera) == "divide":
        print ((a) / (b))
    elif (opera) == "exponent":
        print ((a) ** (b))
    elif (opera) == "remainder":
        print ((a) % (b))
    else:
        print ("Invalid operation.")
    cont = input("Would you like to do another problem?")
    cont = cont.lower()
    if (cont) == "yes":
        ??
    else:
        quit
    

    I want it to restart at the "Type in a value of A." part, but I'm not sure how to do that.

  • rninty
    rninty over 10 years
    … being correctly written in Python as while True: rather than while (true):.
  • jfs
    jfs over 10 years
    you mean: put "your code" in a function and then call the function in a loop. btw, // is not a valid comment in Python.
  • SethMMorton
    SethMMorton over 10 years
    For good measure, at the very least quit would need to be quit(), but it is better to use sys.exit()