报错的代码段
empNum = 0
salarySum= 0
salarys = []
while True:
s = input(“请输入员工的薪资(按 Q或q结束)”)
if s == ‘Q’ or ‘q’:
break
if float(s)<0: continue
empNum +=1
salarys.append(float(s))
salarySum += float(s)
print(“员工数{0}”.format(empNum))
print(“录入薪资:”,salarys)
print(“平均薪资{0}”.format(salarySum/empNum))
错误提示
“C:\python studenty\untitled\venv\Scripts\python.exe” “C:/python studenty/untitled/mypython.py”
请输入员工的薪资(按 Q或q结束)4156
Traceback (most recent call last):
员工数0
录入薪资: []
File “C:/python studenty/untitled/mypython.py”, line 14, in
print(“平均薪资{0}”.format(salarySum/empNum))
ZeroDivisionError: division by zero
Process finished with exit code 1
仅更改使用.upper识别大小写不使用or
empNum = 0
salarySum= 0
salarys = []
while True:
s = input(“请输入员工的薪资(按 Q或q结束)”)
if s.upper() == ‘Q’:
break
if float(s)<0: continue
empNum +=1
salarys.append(float(s))
salarySum += float(s)
print(“员工数{0}”.format(empNum))
print(“录入薪资:”,salarys)
print(“平均薪资{0}”.format(salarySum/empNum))
运行结果
“C:\python studenty\untitled\venv\Scripts\python.exe” “C:/python studenty/untitled/mypython.py”
请输入员工的薪资(按 Q或q结束)3000
请输入员工的薪资(按 Q或q结束)4000
请输入员工的薪资(按 Q或q结束)q
员工数2
录入薪资: [3000.0, 4000.0]
平均薪资3500.0
Process finished with exit code 0
求大神指教,本人小白多多见谅。
本文介绍了一段Python代码,用于统计员工薪资,并计算平均薪资。文章首先展示了出现除以零错误的原始代码,随后通过改进输入终止条件解决了该问题。修正后的程序能够正确处理用户输入并计算平均薪资。
1090

被折叠的 条评论
为什么被折叠?



