第一次计算后如何使该程序重复/循环

时间:2018-12-22 20:39:53

标签: python-3.x loops

我正在尝试使该程序询问用户他们已经计算出一个问题而不只是结束一个问题之后想要计算什么。

我知道我将在这种情况下使用For Loop,但实际上我不确定应如何合并此

#WE ARE TRYING TO SPLIT THE OPTIONS INTO FUNCTIONS THAT CAN BE CALLED LATER DEPENDING ON THE INPUT


# THIS DETERMINES THE COMPOUND INTEREST
def calc_int():
    # SETS THE USER INPUT INTO VARIABLES AS 'int' OR 'float' THAT CAN BE USED IN LATER FORMULAS
    principal = int(input("What is the principal amount?"))
    rate = float(input("What is the rate amount? (Enter in decimal format)"))
    number = int(input("How many times will this be compounded yearly?"))
    time = int(input("How long will this be compounded for?"))

    # CALCULATES THE COMPOUND INTEREST AMOUNT
    exponent = number * time
    interest_amount = principal * (1 + rate / number) ** exponent

    # CALCULATES HOW MUCH WAS EARNED OR LOST
    earned_amount = interest_amount - principal

    # THIS WILL DETERMINE THE COMPOUND AMOUNT FOR EACH TERM (STILL A WORK IN PROGRESS)
    term_1 = principal * rate * time
    new_amount_term_1 = term_1 + principal
    # Term_2 = New_Amount_Term_1 * rate * time_i
    # New_Amount_Term_2 = Term_2 + New_Amount_Term_1

这里的概念是创建取决于用户想要计算的功能的功能。我不需要了所有我想的东西,就省略了所有这些功能。

# LETS FIGURE OUT WHAT THEY WANT TO DO
print("Welcome! What are we calculating today?")
print("Make a wise choice!")
print("1 - Interest")
print("2 - Fahrenheit")
print("3 - Celsius")  # THIS IS THE ONLY DIFFERENT ONE BECAUSE WE ONLY NEED ONE TO RECEIVE INPUT
print("4 - Circumference")  # ALSO THAT WAS THE ONE I ADDED INPUT TO THEN ADDED MORE FUNCTIONS SO YEAH...
print("5 - Area (Circle)")
print("6 - Area (Box)")
Answer_1 = int(input("7 - Perimeter (box)"))


# NOW LETS FIGURE OUT WHAT THEY CHOSE AND MOVE OVER TO THAT SECTION
if Answer_1 == 1:
    calc_int()
elif Answer_1 == 2:
    calc_fahr()
elif Answer_1 == 3:
    calc_cel()
elif Answer_1 == 4:
    calc_cir()
elif Answer_1 == 5:
    calc_area_circle()
elif Answer_1 == 6:
    calc_area_box()
elif Answer_1 == 7:
    calc_per_box()

我需要代码在完成第一个计算后要求进行另一次计算,而不是像现在这样在第一次计算后才结束。

1 个答案:

答案 0 :(得分:1)

如果要继续循环,则需要添加其他条件才能退出。假设用户可以输入0退出。然后,您可以使用以下代码。

while True:
    # LETS FIGURE OUT WHAT THEY WANT TO DO
    print("Welcome! What are we calculating today?")
    print("Make a wise choice!")
    print("1 - Interest")
    print("2 - Fahrenheit")
    print("3 - Celsius")  # THIS IS THE ONLY DIFFERENT ONE BECAUSE WE ONLY NEED ONE TO RECEIVE INPUT
    print("4 - Circumference")  # ALSO THAT WAS THE ONE I ADDED INPUT TO THEN ADDED MORE FUNCTIONS SO YEAH...
    print("5 - Area (Circle)")
    print("6 - Area (Box)")
    Answer_1 = int(input("7 - Perimeter (box)"))


    # NOW LETS FIGURE OUT WHAT THEY CHOSE AND MOVE OVER TO THAT SECTION
    if Answer_1 == 1:
        calc_int()
    elif Answer_1 == 2:
        calc_fahr()
    elif Answer_1 == 3:
        calc_cel()
    elif Answer_1 == 4:
        calc_cir()
    elif Answer_1 == 5:
        calc_area_circle()
    elif Answer_1 == 6:
        calc_area_box()
    elif Answer_1 == 7:
        calc_per_box()
    elif Answer_1 == 0:
        break