尝试根据年龄计算某人需要缴纳的税额
Trying to calculate the amount of tax someone needs to pay based on age
这是我当前的代码,我正在尝试根据某人的年龄计算他们需要支付的税额。计算是正确的,但我总是出错。
#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.")
这是不断弹出的错误:
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'
>>>
当程序结束时,如果 he/she 需要进行另一次计算,我希望它能提示用户。在研究了几个小时试图找到解决方案之后,我是否将整个主要代码移到这个函数中?
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
这应该可以解决问题:
#Get the Age of from user input
from datetime import datetime, date
#define functions before main script
def calculate_age(born):
today = date.today()
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))
#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
#run until close = True
close = False
while close = False:
print("Please enter your date of birth (dd mm yyyy)") #get age input
date_of_birth = datetime.strptime(input("--->"), "%d %m %Y")
age = calculate_age(date_of_birth) #get age from function calculate_age()
print("You are " ,int(age), " years old.")
#Get the Salary from user input
while not salary.is_integer(): #keep asking for salary until it is an int
salary = int(input("Please enter your salary: "))
if not salary.is_integer(): #if the salary inputed is an int it will skip this next step
print("You need to enter a number!")
salary = get_salary() #get salary from get_salary()
tax = contribution(age) #calculate the tax
print("you have to pay", tax, " every month.")
ask = input("Do you need another calculation?") #if ask is yes close will stay as false and the script will just run again
if ask == 'yes' or ask == 'y':
return
elif ask == 'no' or ask == 'n': #if ask is no, close will be set to True closing the script
close = True
这是我当前的代码,我正在尝试根据某人的年龄计算他们需要支付的税额。计算是正确的,但我总是出错。
#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.")
这是不断弹出的错误:
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'
>>>
当程序结束时,如果 he/she 需要进行另一次计算,我希望它能提示用户。在研究了几个小时试图找到解决方案之后,我是否将整个主要代码移到这个函数中?
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
这应该可以解决问题:
#Get the Age of from user input
from datetime import datetime, date
#define functions before main script
def calculate_age(born):
today = date.today()
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))
#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
#run until close = True
close = False
while close = False:
print("Please enter your date of birth (dd mm yyyy)") #get age input
date_of_birth = datetime.strptime(input("--->"), "%d %m %Y")
age = calculate_age(date_of_birth) #get age from function calculate_age()
print("You are " ,int(age), " years old.")
#Get the Salary from user input
while not salary.is_integer(): #keep asking for salary until it is an int
salary = int(input("Please enter your salary: "))
if not salary.is_integer(): #if the salary inputed is an int it will skip this next step
print("You need to enter a number!")
salary = get_salary() #get salary from get_salary()
tax = contribution(age) #calculate the tax
print("you have to pay", tax, " every month.")
ask = input("Do you need another calculation?") #if ask is yes close will stay as false and the script will just run again
if ask == 'yes' or ask == 'y':
return
elif ask == 'no' or ask == 'n': #if ask is no, close will be set to True closing the script
close = True