在控制台中运行python简单代码时遇到问题

时间:2016-12-05 16:58:34

标签: python python-2.7

我运行了这段代码。然后它显示了控制台中的以下内容。

  

回溯(最近一次呼叫最后一次):文件"",第13行,在NameError中:名称' r'未定义

!!

2 个答案:

答案 0 :(得分:2)

有多个错误和缩进问题;我评论他们出错的地方:

p = int(raw_input("Please enter deposit amount: \n"))
r = int(raw_input("Please input interest rate: \n")) #rename i to r
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) #multiply p with ()
B = p*(1+r)**t #same as B

 #** is power

if interest == "simple":
    print (float(A)) # you dont need to cast the float again to int
else: #since there is no other conditions it's obvious to print compound 
    print(float(B))# you dont need to cast the float again to int

答案 1 :(得分:1)

如果您将利率存储在i,那么您不应该在公式中使用r