我正在尝试为计算器提供多个选择。我认为为每种不同的计算创建函数,并根据用户输入,将执行特定的函数。在运行代码并收到用户输入后,它不会按照我希望的方式执行该功能。我要去哪里错了?在此先感谢您抽出宝贵的时间来审查我的工作。
到目前为止,我的代码:
# WE ARE TRYING TO SPLIT THE OPTIONS INTO FUNCTIONS THAT CAN BE CALLED LATER DEPENDING ON THE INPUT
# SETS THE USER INPUT INTO VARIABLES AS 'int' OR 'float' THAT CAN BE USED IN LATER FORMULAS
def calc_int():
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
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
print("The interest amount is:")
print(interest_amount)
print("The amount earned is:")
print(earned_amount)
print("")
print("Term 1 amount is:")
print(term_1)
print("The ending balance for that term is:")
print(new_amount_term_1)
print("")
print("Term 2 amouunt is:")
# print(Term_2)
print("working on it")
print("The ending balance or that term is:")
# print(New_Amount_Term_2)
print('working on it')
def calc_fahr():
# LETS GET THE CELSIUS AMOUNT THAT WE ARE CONVERTING
celsius = int(input("What is the celsius amount?"))
# NOW LETS MAKE THE CALCULATION
fahrenheit = (9 * celsius) / 5 + 32
# PRINTS THE CONVERTED VALUE
print(celsius, "celsius is equal to", fahrenheit, "fahrenheit")
def calc_cel():
# RETRIEVE THE FAHRENHEIT AMOUNT THAT WILL BE CONVERTED
fahrenheit = int(input("What is the fahrenheit amount?"))
# THIS CONVERTS THE INPUTED AMOUNT TO FAHRENHEIT
celsius = (fahrenheit - 32) * 5 / 9
# PRINTS THE CONVERTED VALUE
return fahrenheit, "fahrenheit is equal to", celsius, "celsius"
# LETS FIGURE OUT WHAT THEY WANT TO DO
print("Welcome! What are we calculating today?")
print("Make a wise choice!")
print("0 - Interest")
print("1 - Fahrenheit")
Answer_1 = input("2 - Celsius")
# NOW LETS FIGURE OUT WHAT THEY CHOSE AND MOVE OVER TO THAT SECTION
if Answer_1 == 0:
calc_int()
elif Answer_1 == 1:
calc_fahr()
elif Answer_1 == 2:
calc_cel()