python - 在 for 循环中计算输入
问题描述
变量“countT”会在每次输入后不断添加,即使 if 语句说要向“countT”添加 0,它也会继续这样做。我试过这样做,如果 temp <= 0: 那么它也不会向“countT”添加任何内容。
countT = 0
temp = 0
listT = []
breath = input("Do you have shortness of breath (y/n)? ")
cough = input("Do you have a cough(y/n)? ")
sorethroat_runnynose = input("Do you have a sore throat/runny nose (y/n)? ")
nausea = input("Do you have diarrhea, nausea and vomiting (y/n)? ")
if str.lower(breath)== 'y' and str.lower(cough)== 'y'and str.lower(sorethroat_runnynose)== 'n' and str.lower(nausea)== 'n':
print("Enter 5 most recent temperature readings:")
for x in range (1,6):
temp2= int(input("Temp: "))
temp += temp2
listT.append(temp2)
if temp == 0:
countT +=0
else:
countT +=1
avgT = (temp/countT)
print("Your max Temp was: ",(max(listT)))
print("Average Temp is: ",avgT)
print("You likely have the influenza virus")
if avgT > 100 and temp > max(listT):
print("Please seek medical attention now!")
elif avgT > 99:
print("Monitor your condition closely!")
elif str.lower(breath)== 'n' and str.lower(cough)== 'n' and str.lower(sorethroat_runnynose)== 'n' and str.lower(nausea)== 'n':
print("There are no symptoms but you should wait to see if you develop any.")
else:
print("you likely have the influenza virus.")
解决方案
从你的问题我理解的是,如果当前输入不是 0,你想加 1。countT
它现在不能正常工作的原因是你正在检查语句temp
,if temp == 0:
尽管你应该检查temp2
,因为temp2
是输入并且temp
是总和输入。此外,您可以在一个 if 语句(没有 else)中编写整个检查,如下所示:
if temp != 0:
countT +=1
或者如果您还想消除负面输入:
if temp > 0:
countT +=1
推荐阅读
- r - R 版本 4.0.3 64 64 位在 Windows 10 上崩溃
- json - 如何使用 JMeter 报告生成器生成 JSON 文件
- wpf - 停靠面板中的两个分组框,如何正确设置调整大小(拉伸和固定)?
- python - BMI计算器不输出Python
- lotus-notes - Notes 文档中的新字段(项目)不知何故消失了。复制问题?
- json.net - 如何使用 json.net 将 JSON 子节点解析为数组?
- git - 我什么时候应该使用“git push --force-if-includes”
- reactjs - 使用框架单元渲染器的 Ag 网格会在任何商店更改时不断重新渲染
- isabelle - 要求澄清伊莎贝尔理论中涉及自然数的理论中明显的实强制的出现
- batch-file - 如何在批处理脚本中找到我的错误?