2016-02-28 85 views
-4

我試圖找出一種方法來循環這段代碼,以便它在所有三次計算完成後重新啓動。我已經想出了一種重新啓動程序的方法,但是我無法重新啓動它,以便返回到第一個計算步驟。任何人都可以幫助兄弟出去?提前致謝。在Python中循環

,我已經習慣了重新啓動程序的代碼:

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() 

我沒有重啓代碼程序:

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) 

回答

0

您可以將所有內容放在while循環中,它可以永久重複或直到用戶鍵入某個短語。

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) 

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

你也可以把它全部變成功能def main():,然後做一些形式的遞歸(調用函數本身)。

這些只是幾種方法。有很多方法可以得到你想要的東西。

-2

這應該做的。

def restart_program(): 

python = sys.executable 
os.execl(python, python, * sys.argv) 

if __name__ == "__main__": 
    while input("Do you want to restart this program?").lower().strip() in "y, yes".split(): 
     restart_program() 
-1

只是有自己的功能循環。重新啓動外殼是沒有必要的。

import math 
import sys 
import os 

def calc(): 
    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) 

    if __name__ == "__main__": 
     answer = input("Do you want to restart this program?") 
     if answer.lower().strip() in "y, yes".split(): 
      calc() 
     else: 
      exit() 


calc() 

如果答案是肯定的,那麼函數被調用並且一切都重複。如果答案不是,那麼程序退出。