Python 找到所有变量的总和?
试图找到一种方法来查找该问题中部门所有员工的总基本工资、总佣金和总薪酬。到目前为止,我将此作为代码:Python 找到所有变量的总和?,python,python-3.x,Python,Python 3.x,试图找到一种方法来查找该问题中部门所有员工的总基本工资、总佣金和总薪酬。到目前为止,我将此作为代码: employee = str(1) numbemployee = input('How many employees are there in your department? ') while numbemployee < str(0): print('The number of employees cannot be less than zero!') numbem
employee = str(1)
numbemployee = input('How many employees are there in your department? ')
while numbemployee < str(0):
print('The number of employees cannot be less than zero!')
numbemployee = int(input('How many employees are there in your department? '))
while employee <= numbemployee:
name = str(input('What is the name of employee ' +employee +'? '))
employee = str(int(employee) + 1)
totalyears = int(input('How many years has ' +name +' worked for the company? '))
while totalyears < 0:
print('The number of years worked cannot be less than zero!')
totalyears = int(input('How many years has ' +name +' worked for the company? '))
if totalyears <= 6:
base_salary = 10000.00
print('The base salary for ' ,name ,' should be $' ,base_salary)
elif totalyears <= 13:
base_salary = 14000.00
print('The base salary for ' ,name ,' should be $' ,base_salary)
elif totalyears <= 20:
base_salary = 18000.00
print('The base salary for ' ,name ,' should be $' ,base_salary)
elif totalyears <= 34:
base_salary = 27000.00
print('The base salary for ' ,name ,' should be $' ,base_salary)
else:
base_salary = 30000.00
print('The base salary for ' ,name ,' should be $' ,base_salary)
sales = float(input('What was ' +name +"'s sales for the last quarter? "))
while sales < 0:
print('The sales cannot be less than zero!')
sales = float(input('What was ' +name +"'s sales for the last quarter? "))
if sales <= 3999.99:
commission = 1000
print(name ,'earned $' ,commission ,'from commission for sales from last quarter')
elif sales <= 11999.99:
commission = 2000
print(name ,'earned $' ,commission ,'from commission for sales from last quarter')
elif sales <= 15999.99:
commission = 5000
print(name ,'earned $' ,commission ,'from commission for sales from last quarter')
elif sales <= 23999.99:
commission = 8000
print(name ,'earned $' ,commission ,'from commission for sales from last quarter')
elif sales <= 27999.99:
commission = 12000
print(name ,'earned $' ,commission ,'from commission for sales from last quarter')
else:
commission = 15000
print(name ,'earned $' ,commission ,'from commission for sales from last quarter')
totalwage = base_salary + commission
print(name ,'should recieve $' ,totalwage ,' for this quarter.')
employee=str(1)
numberemployee=input('您的部门有多少员工?')
当numberEmployee 虽然employee您的代码有很多问题-这很好,但这显然是一个学习问题
为了汇总所有基本工资、奖金等,必须为这些金额创建累加器变量。现在,您有了员工详细信息的变量。您需要添加更多变量来处理从一名员工到下一名员工的累计总数:
accum_total_salary = 0
accum_total_commission = 0
while ...
base_salary = ...
commission = ...
accum_total_salary += base_salary
accum_total_commission += commission
最后,在循环之外,当您处理完所有员工信息后,您可以打印您的累积数字:
print("Sum total salary paid to all employees:", accum_total_salary)
print("Sum total commission paid to all employees:", accum_total_commission)
为什么要将输入转换为int,然后将其与str(0)
进行比较?也可以考虑将employee
转化为一个类或至少是一本字典……这个“问题”需要进一步完善。你想干什么?什么是伪代码?你没有预料到会发生什么?也许可以通过一个简单的例子和对你所做尝试的解释,把这变成一个有用的问题。看到了代码不起作用的很大一部分原因是错误地将应该是数字的东西投射到str
。