0

I would like to reset/restart my program but based on a user input. So say if the user types "yes" and clicks enter, the program will reset/restart and start fresh. I am making a calculator for energy; Physics problems. The out puts are Mechanical Energy, Kinetic Energy, and Potential Energy. All the formulas are coded properly, everything is working as expected, except I have no way to restart the program without clicking back on the file and reopening.

#Importing "time" and "math" so I can use the time.sleep and rounding functions
import time
import math
restart=1
user_r1=float(input ("What is the mass of your object (kg) = "))
print ("")
user_r2=float(input ("Whats is the height you are dropping from (m) = "))
print ("")
user_r3=float(input ("What is the velocity (m/s) = "))
#Formulas that solve from the inputs given
EKinetic = 0.5 * user_r1 * user_r3 * user_r3
EPotential = user_r1 * 9.81 * user_r2
MechEnergy = (EPotential) + (EKinetic)
#Rounding - To change the ammount of sigdigs change the # infront of the f'
EKinetic='%.1f' % EKinetic
EPotential='%.1f' % EPotential
MechEnergy='%.1f' % MechEnergy
#Spitting out the info the user will see
print ("")
print ("===============================================================================")
print ("Kinetic Energy = ",EKinetic,"J")
print ("")
print ("Potential Energy = ",EPotential,"J")
print ("")
print ("Mechanical Energy = ",MechEnergy,"J")
print ("===============================================================================")
print ("")
#This time.sleep will make the program pause for (x# of seconds)
time.sleep(3)
user_r4=(input ("To do another calculation type yes and hit enter = "))
if user_r4 == ("yes"):
    print ("YAY")
    ###PUT RESTART CODE HERE###
    ###EVERYTHING DOWN ARE JUST PLACE HOLDERS FOR THE RESTART CODE###
else:
    print ("Please type yes to continue")
time.sleep(1)
user_r4=(input ("To do another calculation type yes and hit enter = "))
if user_r4 == ("yes"):
    print ("YAY")
    ###PUT RESTART CODE HERE###
else: print ("Thats enough!!!")
time.sleep(3)
2
  • 1
    Is there a reason that you can't use a simple loop? Commented Feb 13, 2014 at 3:10
  • Or you use a goto. Commented Feb 13, 2014 at 3:21

1 Answer 1

3

What you're looking for is a simple input loop

while True:
    # Existing code here

    user_r4= input ("To do another calculation type yes and hit enter = ")

    # Exit the loop if the user does not want to proceed
    if len(user_r4) and user_r4[0] in "Yy":
        break
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.