I am very new to Python and trying to execute below code. The program is to calculate compound interest.
I created basic program and passed values as argument and it worked. Later I created user input program and it worked.
Now when I tried to handle negative or 0 values I am not able to exit program
def CompI(amt, r, t):
return amt * (pow((1 + r / 100), t))
print("Enter Amount")
amt = int(input())
if amt<0 or amt == 0:
print("Invalid Input")
exit()
print("Enter rate")
r = int(input())
if r<0 or r == 0:
print("Invalid Input")
exit()
print("Enter Time")
t = int(input())
if t<0 or t == 0:
print("Invalid Input")
exit()
CI = CompI(amt, r, t)
print("Result is", CI)
Result
Enter Amount 0 Invalid Input Enter rate
when I enter amount as 0 it displays error message and further ask me to enter rate.
maybe try import sys
and then use sys.exit()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.