Skip to content Skip to sidebar Skip to footer

Having Trouble With Python Simple Code Running In Console

I ran this code.Then it displayed the follwing in the console. Traceback (most recent call last): File '', line 13, in NameError: name 'r' is not defined import math p = int(ra

Solution 1:

There were multiple errors and indentation problem ;I commented them where you made mistakes:

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 powerif interest == "simple":
    print (float(A)) # you dont need to cast the float again to intelse: #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

Solution 2:

If you store the interest rate in i you should not use r in the formula some lines later.

Post a Comment for "Having Trouble With Python Simple Code Running In Console"