I'm trying to make a program to calculate compound interest to figure out keyword arguments for functions, by accepting inputs for the principal, time, and rate, in Python.
After checking if the entered values are not blank and are numeric, I use eval to use them as Named Arguments for the function, but I get SyntaxError: invalid syntax
.
Could I get help with where I'm going wrong?
The code:
def compound(principal = 10000, time = 20, rate = 4):
amt = principal * (((rate/100) + 1) ** time)
return amt
p = input("Enter principal amount: ")
r = input("Enter the rate of interest per annum: ")
t = input("Enter the time period(in years): ")
arg = []
if p != "" and p.isnumeric():
arg += ["principal = " + p]
if r != "" and r.isnumeric():
arg += ["rate = " + r]
if t != "" and t.isnumeric():
arg += ["time = " + t]
print(compound(eval(", ".join(arg))))
Output:
Enter principal amount: 1000
Enter the rate of interest per annum: 4.5
Enter the time period(in years): 10
Traceback (most recent call last):
File "main.py", line 18, in <module>
print(compound(eval(", ".join(arg))))
File "<string>", line 1
principal = 1000, time = 10
^
SyntaxError: invalid syntax