Looping in Python
Question: I'm trying to figure a way to loop this code so that it restarts once all
three of the calculations are complete. I have figured a way to restart the
program itself, however I can't manage to restart it so that it returns back
to the first calculation step. Anyone can help a brother out? Thanks in
advance.

The code that I have used to restart the program:

    
    
    def restart_program():
    
    python = sys.executable
    os.execl(python, python, * sys.argv)
    
    if __name__ == "__main__":
        answer = input("Do you want to restart this program?")
        if answer.lower().strip() in "y, yes".split():
            restart_program()
    

My program without the restart code:

    
    
    import math
    import sys
    import os
    
    
    print ("This program will calculate the area, height and perimeter of the Triangles: Scalene, Isosceles, Equilateral and a Right Angled Triangle.")
    
    # calculate the perimeter
    
    print ("Please enter each side for the perimeter of the triangle")
    
    a = float(input("Enter side a: "))
    b = float(input("Enter side b: "))
    c = float(input("Enter side c "))
    
    perimeter = (a + b + c)
    
    print ("The perimeter for this triangle is: " ,perimeter)
    
    
    # calculate the area
    
    print ("Please enter each side for the area of the triangle")
    
    a = float(input("Enter side a: "))
    b = float(input("Enter side b: "))
    c = float(input("Enter side c "))
    
    s = (a + b + c) / 2
    
    sp = (a + b + c) / 2
    area = (s*(s-a)*(s-b)*(s-c)) ** 0.5    #area = math.sqrt(sp*(sp - a)*(sp - b)*(sp - c))#
    
    print ("The area for this triangle is %0.2f: " %area)
    
    
    # calculate the height
    
    height = area / 2
    
    print ("The height of this triangle is: ", height)
    


Answer: You could put everything in a while loop which could repeat forever or until a
user types a certain phrase.

    
    
    import math
    import sys
    import os
    
    
    print ("This program will calculate the area, height and perimeter of the Triangles: Scalene, Isosceles, Equilateral and a Right Angled Triangle.")
    while True:
    
    
        # calculate the perimeter
    
        print ("Please enter each side for the perimeter of the triangle")
    
        a = float(input("Enter side a: "))
        b = float(input("Enter side b: "))
        c = float(input("Enter side c "))
    
        perimeter = (a + b + c)
    
        print ("The perimeter for this triangle is: " ,perimeter)
    
    
        # calculate the area
    
        print ("Please enter each side for the area of the triangle")
    
        a = float(input("Enter side a: "))
        b = float(input("Enter side b: "))
        c = float(input("Enter side c "))
    
        s = (a + b + c) / 2
    
        sp = (a + b + c) / 2
        area = (s*(s-a)*(s-b)*(s-c)) ** 0.5    #area = math.sqrt(sp*(sp - a)*(sp - b)*(sp - c))#
    
        print ("The area for this triangle is %0.2f: " %area)
    
    
        # calculate the height
    
        height = area / 2
    
        print ("The height of this triangle is: ", height)
    

or

    
    
    while answer.lower() in ("yes", "y"):
        //code
        answer = input("Would you like to repeat?")
    

You could also put it all into a function `def main():` and then do some form
of recursion (calling a function in itself).

Those are just a few ways. There are a ton of ways you can get what you want.

