您的位置:首页 > 编程语言 > Python开发

关于python中if语句中or使用异常退出循环,

2018-08-15 09:34 330 查看

报错的代码段
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

求大神指教,本人小白多多见谅。

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: