这几乎是完美的,只是计算得出错误的结果,这是因为它将答案除以100 我必须将利率除以100.因此,用户输入的r值需要除以100
import math
p = int(raw_input("Please enter deposit amount: \n"))
r = int(raw_input("Please input interest rate: \n")/100)
t = int(raw_input("Please insert number of years of the investment: \n"))
interest = raw_input("Do you want a simple or compound interest ? \n")
A = p*(1+r**t)
B = p*(1+r)^t
if interest == "simple":
print (A)
else:
print(B)
答案 0 :(得分:0)
所以,你只需要改变这一行:
r = int(raw_input("Please input interest rate: \n")/100)
要:
r = int(raw_input("Please input interest rate: \n"))/100
raw_input("Please input interest rate: \n")/100
表示您将结果转换为int将字符串除以100。相反,您需要将用户输入转换为int
并将其除以100。
注意:在Python 2中,10/100
例如返回0
,因此您需要除100.0
而不是100
。
>>> 10/100
0
>>> 10/100
0
>>> 10/100.0
0.1
答案 1 :(得分:0)
import math
p = int(input("Please enter deposit amount: \n"))
r = int(input("Please input interest rate: \n"))/100
t = int(input("Please insert number of years of the investment: \n"))
interest = input("Do you want a simple or compound interest ? \n")
A = p*(1+r**t)
B = p*(1+r)**t
if interest == "simple":
print (A)
elif interest == "compound":
print(B)
else:
print("Error on the interest type")