我从这段代码中得到一些错误,有人能解释一下这似乎是什么问题吗?谢谢!该计划应该计算申报(柜台)员工数量的工资单。
我从下面的代码中得到一些错误,有人可以解释一下这似乎是什么问题吗?谢谢!
while (True):
status = eval(input("Are they single(1) or married(2)? \n"))
if status != 1 or 2:
break
注意:这是Python 3.3
以下是其余代码。 另外:请随意进行任何改进!我正在学习!
def computeGrossPay(hours, rate):
if hours > 40:
grossPay = (40 * rate) + ((hours - 40) * 1.5 * hours)
else:
grossPay = hours * rate
return grossPay
def computeTaxAmount(status, grossPay):
if status == 1:
if grossPay <= 42:
taxAmount = 0
elif grossPay > 42 and grossPay <= 214:
taxAmount = (grossPay - 15) * .1
elif grossPay > 214 and grossPay <= 739:
taxAmount = 17.2 + ((grossPay - 214) * .15)
elif grossPay > 739 and grossPay <= 1732:
taxAmount = 95.95 + ((grossPay - 739) * .28)
elif grossPay > 1732 and grossPay <= 2566:
taxAmount = 344.2 + ((grossPay - 1732) * .28)
elif grossPay > 3566 and grossPay <= 7703:
taxAmount = 857.72 + ((grossPay - 3566) * .33)
elif grossPay > 7703 and grossPay <= 7735:
taxAmount = 2222.93 + ((grossPay -7703) * .35)
elif grossPay > 7735:
taxAmount = 2234.13 + ((grossPay - 7735) * .396)
elif status == 2:
if grossPay <= 160:
taxAmount = 0
elif grossPay > 160 and grossPay <= 503:
taxAmount = (grossPay - 160) * .1
elif grossPay > 503 and grossPay <= 1554:
taxAmount = 34.3 + ((grossPay - 503) * .15)
elif grossPay > 1554 and grossPay <= 2975:
taxAmount = 191.95 + ((grossPay - 1554) * .28)
elif grossPay > 2975 and grossPay <= 4449:
taxAmount = 547.2 + ((grossPay - 2975) * .28)
elif grossPay > 4449 and grossPay <= 7820:
taxAmount = 959.92 + ((grossPay - 4449) * .33)
elif grossPay > 7820 and grossPay <= 8813:
taxAmount = 2072.35 + ((grossPay -7820) * .35)
elif grossPay > 8813:
taxAmount = 2419.9 + ((grossPay - 8813) * .396)
return taxAmount
def computeNetPay(grossPay, taxAmount):
netPay = grossPay - taxAmount
return netPay
def main():
totalGrossPay = 0
print("Welcome to the Payroll Program")
counter = eval(input("How many employees will be entered? \n"))
for i in range(counter):
name = input("What are the first and last names of the employee? \n")
id = input("What are the last four numbers of his/her Social Security Number? \n")
hours = eval(input("How many hours did they work? \n"))
rate = eval(input("What was the rate? \n"))
while (True):
status = eval(input("Are they single(1) or married(2)? \n"))
if status != 1 or 2:
break
grossPay = computeGrossPay(hours, rate)
taxAmount = computeTaxAmount(status, grossPay)
netPay = computeNetPay(grossPay, taxAmount)
totalGrossPay = totalGrossPay + grossPay
averageGrossPay = totalGrossPay / counter
print("The ID is ", id)
print("The name is ", name, ".")
print("This employee worked ", hours, " hours.")
print("The hourly rate was ", rate)
print("The gross pay was $", grossPay)
print("The tax amount withheld for the week is $", taxAmount)
print("The net pay is $", netPay)
print("The total gross pay was $", totalGrossPay)
print("The average gross pay was $", averageGrossPay)
main()
答案 0 :(得分:2)
您应该像这样使用or
运算符
if status == 1 or status == 2:
更优雅
if status in (1, 2):
,因为
if status != 1 or 2:
表示status
不应等于1
或仅数字2
。即使status == 1
,数值2将被视为True,因此将满足条件并且循环将始终中断。
答案 1 :(得分:1)
or
不起作用。编写所需内容的正确Python方法是:
while True:
status = input("Are they single(1) or married(2)? \n")
try:
status = int(status)
except ValueError:
# You should print a message here.
pass
else:
if status in (1, 2):
break
避免使用eval()
进行输入,因为任何人都可以输入任意(并且可能有害)Python,而python会eval()
使用它。