Подсчет входов в цикле for - PullRequest
0 голосов
/ 29 февраля 2020

Переменная «countT» продолжает добавляться после каждого ввода, даже с оператором if, который говорит, что нужно добавить 0 к «countT», она продолжает это делать. Я попытался сделать, если temp <= 0: тогда это также ничего не добавит к "countT". </p>

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.")

1 Ответ

0 голосов
/ 01 марта 2020

Из вашего вопроса я понял, что вы хотите добавить 1 к countT, если текущий ввод не равен 0. Причина, по которой он сейчас работает неправильно, заключается в том, что вы проверяете temp в if temp == 0: операторе, хотя Вы должны проверить temp2, потому что temp2 - это вход, а temp - это сумма входов. Кроме того, вы можете написать всю проверку в одном операторе if (без других), как показано ниже:

if temp != 0:
    countT +=1

или, если вы также хотите исключить отрицательные входные данные:

if temp > 0:
    countT +=1
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...