This is my current code, and I'm trying to calculate the amount of tax someone needs to pay based on their age. The calculations are right, but I keep getting errors.
#Get the Age of from user input
from datetime import datetime, date
print("Please enter your date of birth (dd mm yyyy)")
date_of_birth = datetime.strptime(input("--->"), "%d %m %Y")
def calculate_age(born):
today = date.today()
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))
age = calculate_age(date_of_birth)
print("You are " ,int(age), " years old.")
#Get the Salary from user input
def get_salary():
while True:
try:
salary = int(input("Please enter your salary: "))
except ValueError:
print("You need to enter a number ")
else:
break
return salary
#Calculate the Amount that needs to be paid, using if,elif,else
def contribution(age):
if age <= 35:
tax = (salary * 0.20)
elif 36 <= age <= 50:
tax = (salary * 0.20)
elif 51 <= age <= 55:
tax = (salary * 0.185)
elif 56 <= age <= 60:
tax = (salary * 0.13)
elif 61 <= age <= 65:
tax = (salary * 0.075)
else:
tax = (salary * 0.05)
return tax
#Print the amount
if __name__ == "__main__":
salary = get_salary
tax = contribution(age)
print("you have to pay", tax, " every month.")
This is the error that keeps popping up:
Please enter your date of birth (dd mm yyyy)
--->01 08 1946
You are 74 years old.
Traceback (most recent call last):
File "/Users/mikael/Age-TaxCalculator.py", line 46, in <module>
tax = contribution(age)
File "/Users/mikael/Age-TaxCalculator.py", line 39, in contribution
tax = (salary * 0.05)
TypeError: unsupported operand type(s) for *: 'function' and 'float'
>>>
When the program ends, I would like it to prompt the user if he/she needs to do another calculation. After researching for hours trying to find a solution, do I move the whole main code into this function?
while True:
#input the whole code
while True:
answer = str(input("Do you need to do another calculation? (y/n): "))
if answer in ("y", "n"):
break
print "invalid input."
if answer == "y":
continue
else:
print("Thank you, Goodbye")
break
Aucun commentaire:
Enregistrer un commentaire