它曾经可以工作,现在却不起作用!我还在学习。我正在尝试制作一个复利计算器,这样我就可以开始学习代码了。我不太确定为什么在要求利率后代码会跳到最后...我已经尝试了一段时间了,但我不知道。我敢肯定它很小。我是一个初学者,所以如果有人可以帮助我,那将意味着很多。
import math
class error: # All possible errors are located here.
def input():
print("\nError: Invalid input entered.\n")
def accumulated():
print("\nError: There has been an error calculating the accumulated amount.\n")
def principal():
print("\nError: There has been an error calculating the principal amount.\n")
def rate():
print("\nError: There has been an error calculating the interest rate.\n")
def years():
print("\nError: There has been an error calculating the number of years.\n")
class user_input:
def r_of_c(): # What gets assigned here is based upon the user's input based on its corrosoponding function.
roc = int(input("\nEnter the rate of compounding: "))
if roc == (1):
n = 365
elif roc == (2):
n = 52
elif roc == (3):
n = 12
elif roc == (4):
n = 4
elif roc == (5):
n = 2
elif roc == (6):
n = 1
elif roc == (7):
n = 2.7182
elif roc > 7:
error.input()
elif roc < 1:
error.input()
else:
error.input()
def one():
if var == (1):
p = float(input("\nPlease enter the Principal amount: $"))
r = float(input("\nPlease enter an Interest rate: %"))
print("\n[1] Daily [2] Weekly [3] Monthly [4] Quarterly [5] Semi-Annually [6] Annually [7] Continuously")
r_of_c()
t = int(input("\nPlease enter the number of years compounded: "))
try: accumulated_amount = float(p * (1 + ((r/100)/n)) ** (n * t))
except:
error.accumulated()
else:
pass
def two():
if var == (2):
a = float(input("\nPlease enter the Accumulated amount: $"))
r = float(input("\nPlease enter an Interest rate: %"))
print("\n[1] Daily [2] Weekly [3] Monthly [4] Quarterly [5] Semi-Annually [6] Annually [7] Continuously")
r_of_c()
t = int(input("\nPlease enter the number of years compounded: "))
try: principal_amount = float(a / (1 + ((r/100)/n)) ** (n * t))
except:
error.principal()
else:
pass
def three():
if var == (3):
a = float(input("\nPlease enter the Accumulated amount: $\n"))
p = float(input("\nPlease enter the Principal amount: $"))
print("\n[1] Daily [2] Weekly [3] Monthly [4] Quarterly [5] Semi-Annually [6] Annually [7] Continuously")
r_of_c()
t = int(input("\nPlease enter the number of years compounded: "))
try: interest_rate = float(n(math.pow(a/p, 1/nt) - 1))
except:
error.rate()
else:
pass
def four():
if var == (4):
a = float(input("\nPlease enter the Accumulated amount: $\n"))
p = float(input("\nPlease enter the Principal amount: $"))
r = float(input("\nPlease enter an Interest rate: %"))
print("\n[1] Daily [2] Weekly [3] Monthly [4] Quarterly [5] Semi-Annually [6] Annually [7] Continuously")
r_of_c()
t = int(input("\nPlease enter the number of years compounded: "))
try: number_of_years = ((math.log10(a/p) / n(math.log10(1 + (r/100)/n))))
except:
error.years()
else:
pass
def greater_than():
if var > 4:
error.input()
else:
pass
def less_than():
if var < 1:
error.input()
else:
pass
class answer: # This will be processed based upon the intial user input.
def one():
if var == (1):
try: print("\nThe accumulated amount is $%s.\n" % round(accumulated_amount, 2))
except:
pass
def two():
if var == (2):
try: print("\nThe principal amount is $%s.\n" % round(principal_amount, 2))
except:
pass
def three():
if var == (3):
try: print("\nThe interest rate is $%s.\n" % interest_rate)
except:
pass
def four():
if var == (4):
try: print("\nThe number of years compounded is $%s.\n" % round(number_of_years, 2))
except:
pass
print("[1] Accumulated Amount [2] Principal [3] Interest Rate [4] Years \n")
var = int(input("What do you want to solve for? "))
""" The following will process the users input based on which function it gets assigned to. """
try: user_input.one()
except:
pass
try: user_input.two()
except:
pass
try: user_input.three()
except:
pass
try: user_input.four()
except:
pass
try: user_input.greater_than()
except:
pass
try: user_input.less_than()
except:
pass
""" The following will output the answer based on the initial user input. """
try: answer.one()
except:
pass
try: answer.two()
except:
pass
try: answer.three()
except:
pass
try: anwer.four()
except:
pass
input("Press Enter to exit. ")
答案 0 :(得分:0)
它似乎跳到最后的原因是因为<div class="mt-3" />
的大多数方法,例如class user_input
,one()
,two()
等都试图调用其{ {1}}方法导致
three()
由于未正确完成而发生异常。
这样可以执行以下结尾处的行:
r_of_c()
每个在NameError: name 'r_of_c' is not defined
/ try: user_input.one()
except:
pass
try: user_input.two()
except:
pass
try: user_input.three()
except:
pass
try: user_input.four()
except:
pass
try: user_input.greater_than()
except:
pass
try: user_input.less_than()
except:
pass
""" The following will output the answer based on the initial user input. """
try: answer.one()
except:
pass
try: answer.two()
except:
pass
try: answer.three()
except:
pass
try: anwer.four()
except:
pass
input("Press Enter to exit. ")
内对user.input.xxx()
的调用遇到引发的异常,但是它们被忽略,并且尝试进行以下调用,依此类推依此类推,直到遇到try
。
我认为正确的调用方式是except
而不是普通的input("Press Enter to exit. ")
。请注意,由于还有很多其他问题需要解决,仅使此更改不足以使其起作用,但应停止跳到最后。