Python 我应该使用数据结构而不是使用原始方法吗?
这是我当前基于年龄的税务计算器代码。我认为如果我在计算括号时使用数据结构,将来更新会更容易。有人能帮我弄明白吗Python 我应该使用数据结构而不是使用原始方法吗?,python,if-statement,data-structures,while-loop,Python,If Statement,Data Structures,While Loop,这是我当前基于年龄的税务计算器代码。我认为如果我在计算括号时使用数据结构,将来更新会更容易。有人能帮我弄明白吗 while True: #Loop the whole program from datetime import datetime, date #Get the Age of from user input print("Please enter your date of birth (dd mm yyyy)") date_of_b
while True: #Loop the whole program
from datetime import datetime, date #Get the Age of from user input
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__": # It's as if the interpreter inserts this at the top of your module when run as the main program.
salary = get_salary() #get salary from get_salary()
tax = contribution(age) #calculate the tax
print("you have to pay", tax, " every month ")
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 for using this Calculator, Goodbye")
break
为True时:#循环整个程序
从datetime导入datetime,date#从用户输入中获取的年龄
打印(“请输入您的出生日期(dd-mm-yyyy)”)
出生日期=datetime.strTime(输入(“-->”,%d%m%Y))
def计算年龄(出生):
今天=日期。今天()
返回今日.year-born.year-((今日.month,今日.day)<(今日.month,今日.day))
年龄=计算年龄(出生日期)
打印(“你是”,整数(年龄),“岁。”)
#从用户输入中获取工资
def get_salary():
尽管如此:
尝试:
工资=int(输入(“请输入您的工资:”)
除值错误外:
打印(“您需要输入一个数字”)
其他:
打破
返回工资
#使用if、elif和else计算需要支付的金额
def供款(年龄):
如果是1岁。重写代码结构
首先,我认为大多数程序员都喜欢将函数块放在一起,并尽可能保持主逻辑干净/简短,以提高可读性。因此,像这样的“代码结构”将来可能很难维护或更新
while True: #Loop the whole program
from datetime import datetime, date #Get the Age of from user input
def calculate_age(born): ...
def get_salary(): ...
def contribution(age): ...
if __name__ == "__main__":
# main logic
...
所以这种结构真的很奇怪,而且在函数之间声明了很多变量(出生日期
,年龄
)。很难进行更新/维护。
如果我是你,我会先这样修改代码
from datetime import datetime, date #Get the Age of from user input
def calculate_age(born): ...
def get_salary(): ...
def contribution(age): ...
if __name__ == "__main__": # It's as if the interpreter inserts this at the top of your module when run as the main program.
program_continue = 'y'
while program_continue.upper() in ['Y', 'YES']:
print("Please enter your date of birth (dd mm yyyy)")
date_of_birth = datetime.strptime(input("--->"), "%d %m %Y")
age = calculate_age(date_of_birth)
print("You are " ,int(age), " years old.")
salary = get_salary() #get salary from get_salary()
tax = contribution(age) #calculate the tax
print("you have to pay", tax, " every month ")
program_continue = str(input("Do you need to do another calculation? (y/n): "))
print("Thank you for using this Calculator, Goodbye")
2.介绍数据结构?还是上课?
老实说,我不太明白你所说的“使用数据结构”是什么意思,所以我想制作一个“类”是你想要的。那么你必须考虑一些要点:
- 这个类的属性应该是什么?dob,薪水,还有别的吗
- 你将来会扩展什么?名称性别?联系方式?残障与否
不管怎样,我们现在只创建一个包含dob和薪水的类
class Person(object):
def __init__(self, dob, salary):
"""
input dob and salary only, age and tax will be calculated then
"""
self.dob = dob
self.salary = salary
self.age = self.calculate_age()
self.tax = self.contribution()
def calculate_age(self):
today = date.today()
return today.year - self.dob.year - ((today.month, today.day) < (self.dob.month, self.dob.day))
def contribution(self):
if self.age <= 35:
tax = (self.salary * 0.20)
elif 36 <= self.age <= 50:
tax = (self.salary * 0.20)
elif 51 <= self.age <= 55:
tax = (self.salary * 0.185)
elif 56 <= self.age <= 60:
tax = (self.salary * 0.13)
elif 61 <= self.age <= 65:
tax = (self.salary * 0.075)
else:
tax = (self.salary * 0.05)
return tax
3.未来扩张
假设我现在想添加name
作为属性。我所要做的就是修改Person
类。将来应该更容易更新。1。重写代码结构
while True: #Loop the whole program
from datetime import datetime, date #Get the Age of from user input
def calculate_age(born): ...
def get_salary(): ...
def contribution(age): ...
if __name__ == "__main__":
# main logic
...
首先,我认为大多数程序员都喜欢将函数块放在一起,并尽可能保持主逻辑干净/简短,以提高可读性。因此,像这样的“代码结构”将来可能很难维护或更新
while True: #Loop the whole program
from datetime import datetime, date #Get the Age of from user input
def calculate_age(born): ...
def get_salary(): ...
def contribution(age): ...
if __name__ == "__main__":
# main logic
...
所以这种结构真的很奇怪,而且在函数之间声明了很多变量(出生日期
,年龄
)。很难进行更新/维护。
如果我是你,我会先这样修改代码
from datetime import datetime, date #Get the Age of from user input
def calculate_age(born): ...
def get_salary(): ...
def contribution(age): ...
if __name__ == "__main__": # It's as if the interpreter inserts this at the top of your module when run as the main program.
program_continue = 'y'
while program_continue.upper() in ['Y', 'YES']:
print("Please enter your date of birth (dd mm yyyy)")
date_of_birth = datetime.strptime(input("--->"), "%d %m %Y")
age = calculate_age(date_of_birth)
print("You are " ,int(age), " years old.")
salary = get_salary() #get salary from get_salary()
tax = contribution(age) #calculate the tax
print("you have to pay", tax, " every month ")
program_continue = str(input("Do you need to do another calculation? (y/n): "))
print("Thank you for using this Calculator, Goodbye")
2.介绍数据结构?还是上课?
老实说,我不太明白你所说的“使用数据结构”是什么意思,所以我想制作一个“类”是你想要的。那么你必须考虑一些要点:
- 这个类的属性应该是什么?dob,薪水,还有别的吗
- 你将来会扩展什么?名称性别?联系方式?残障与否
不管怎样,我们现在只创建一个包含dob和薪水的类
class Person(object):
def __init__(self, dob, salary):
"""
input dob and salary only, age and tax will be calculated then
"""
self.dob = dob
self.salary = salary
self.age = self.calculate_age()
self.tax = self.contribution()
def calculate_age(self):
today = date.today()
return today.year - self.dob.year - ((today.month, today.day) < (self.dob.month, self.dob.day))
def contribution(self):
if self.age <= 35:
tax = (self.salary * 0.20)
elif 36 <= self.age <= 50:
tax = (self.salary * 0.20)
elif 51 <= self.age <= 55:
tax = (self.salary * 0.185)
elif 56 <= self.age <= 60:
tax = (self.salary * 0.13)
elif 61 <= self.age <= 65:
tax = (self.salary * 0.075)
else:
tax = (self.salary * 0.05)
return tax
3.未来扩张
假设我现在想添加name
作为属性。我所要做的就是修改Person
类。将来应该更容易更新。您的函数很容易维护,特别是如果您将其重写得更简单一些:
while True: #Loop the whole program
from datetime import datetime, date #Get the Age of from user input
def calculate_age(born): ...
def get_salary(): ...
def contribution(age): ...
if __name__ == "__main__":
# main logic
...
def contribution(age):
if age <= 35:
rate = 0.20
elif age <= 50:
rate = 0.20
elif age <= 55:
rate = 0.185
elif age <= 60:
rate = 0.13
elif age <= 65:
rate = 0.075
else:
rate = 0.05
return salary * rate
def供款(年龄):
如果年龄您的函数相当容易维护,尤其是如果您将其重写得稍微简单一点:
def contribution(age):
if age <= 35:
rate = 0.20
elif age <= 50:
rate = 0.20
elif age <= 55:
rate = 0.185
elif age <= 60:
rate = 0.13
elif age <= 65:
rate = 0.075
else:
rate = 0.05
return salary * rate
def供款(年龄):
如果你愿意,谢谢你!是的,我同意,这会让事情变得更简单。谢谢!是的,我同意,这会让事情变得更简单。