控制台简单计算年薪
def calculate_annual_salary(monthly_salaries):
"""
计算年薪。
参数:
monthly_salaries: list of float,每个月的工资列表。
返回值:
float,用户的年薪。
"""
annual_salary = sum(monthly_salaries)
return annual_salary
def main():
# 从控制台输入每个月的工资
monthly_salaries = []
for month in range(1, 13):
salary = input(f"请输入您第{month}个月的工资: ")
try:
salary = float(salary)
monthly_salaries.append(salary)
except ValueError:
print("输入无效,请输入有效的数字!")
return
# 计算年薪
annual_salary = calculate_annual_salary(monthly_salaries)
# 打印输出年薪
print(f"您的年薪为: {annual_salary:.2f}")
if __name__ == "__main__":
main()
执行结果