How to print multiple lines of input given from a while loop?

Viewed 71

For a bit of context, the user enters patient number, and 3 body temps, and then those 3 temps are averaged and a diagnosis is given. Problem is I cannot get all of the inputs to be printed and not sure how to fix it.

average = 0
total_patient = 0
total = 0
i = 0
fever = 0 
chilled = 0 
avg_list = []
PT_list = {}
diagnosis = ('')
print()    
print('Enter patient number and three temperature readings')
print('Enter blank line to stop entering data')
patient = input('Enter patient: ')
while patient != '':
    avg_list = patient.replace(',', ' ') 
    if patient != ('\n'):
        total_patient += 1
    total = avg_list.split()
    average = (float(total[1]) + float(total[2]) + float(total[3])) / 3
    patient = input('Enter patient: ') 
    PT_list[i] = total[0], average, diagnosis
print()
print('PT      AVG      Diagnosis')
for num in PT_list:
    if average > 98.7:
        diagnosis = ('fever')
        fever += 1
    elif average < 95.0:
        diagnosis = ('chilled')
        chilled += 1
    else:
        diagnosis = ('')         
    print(total[0], '  ', round(average, 2), '   ', diagnosis)
3 Answers

You only need to increment i after this line PT_list[i] = total[0], average, diagnosis. the edited version of code:

average = 0
total_patient = 0
total = 0
i = 0
fever = 0 
chilled = 0 
avg_list = []
PT_list = {}
diagnosis = ('')
print()    
print('Enter patient number and three temperature readings')
print('Enter blank line to stop entering data')
patient = input('Enter patient: ')
while patient != '':
    avg_list = patient.replace(',', ' ') 
    if patient != ('\n'):
        total_patient += 1
    total = avg_list.split()
    average = (float(total[1]) + float(total[2]) + float(total[3])) / 3
    patient = input('Enter patient: ') 
    PT_list[i] = total[0], average, diagnosis
    i += 1  # <=== check this line
print()
print('PT      AVG      Diagnosis')
for num in PT_list:
    if average > 98.7:
        diagnosis = ('fever')
        fever += 1
    elif average < 95.0:
        diagnosis = ('chilled')
        chilled += 1
    else:
        diagnosis = ('')         
    print(total[0], '  ', round(average, 2), '   ', diagnosis)
total_patient = fever = chilled = 0
PT_list = []
print('Enter patient number and three temperature readings')
print('Enter blank line to stop entering data')
while True:
    patient = input('Enter patient: ')
    if patient == "": break
    avg_list = patient.replace(',', ' ') 
    total_patient += 1
    total = avg_list.split()
    average = (float(total[1]) + float(total[2]) + float(total[3])) / 3
    PT_list.append((total[0], average))
    
print('PT      AVG      Diagnosis')
for PT in PT_list:
    if PT[1] > 98.7:
        diagnosis = 'fever'
        fever += 1
    elif PT[1] < 95.0:
        diagnosis = 'chilled'
        chilled += 1
    else:
        diagnosis = ''         
    print(PT[0], '  ', round(PT[1], 2), '   ', diagnosis)

Use a list instead of a dict, then you can just append to it instead of having to keep track of (and increment) a key value.

patient_data = []
print()    
print('Enter patient number and three temperature readings')
print('Enter blank line to stop entering data')
while True:
    patient = input('Enter patient: ').replace(",", " ")
    if not patient:
        break
    num, *temps = patient.split()
    average = sum(float(t) for t in temps) / len(temps)
    patient_data.append((num, average))

print()
print('PT        AVG       Diagnosis')
for num, average in patient_data:
    if average > 98.7:
        diagnosis = 'fever'
    elif average < 95.0:
        diagnosis = 'chilled'
    else:
        diagnosis = ''
    print(f"{num:<10}{average:<10}{diagnosis}")

Enter patient number and three temperature readings
Enter blank line to stop entering data
Enter patient: 123,99,101,100
Enter patient: 234,98,99,99
Enter patient: 345,97,96,95
Enter patient:

PT        AVG       Diagnosis
123       100.0     fever
234       98.67
345       96.0
Related