'how do I print multiple strings that were entered into a while loop, and then split using .split into a list? using python
I am having trouble printing all of the data points entered in a while loop. Some context, user enters patient number, and 3 different body temps on each new line of the while loop. Each of the entered data is split using .split Problem is I can only get one of these entered lines to print.
I've slimmed it down to make it easier to read but here is what I have tried.
PT_list = {}
while patient != '':
....
....
....
PT_list[i] = total[0], average, diagnosis
for num in PT_list:
print(total[0], round(average, 2), diagnosis)
any help would be greatly appreciated
Solution 1:[1]
So, basically, you're printing only the last patient (from what I can understand). You're not actually accessing the data you've stored in your PT_list (by using the while-loop.
What you want to do is to iterate over each element in your dictionary (PT_list) and access its data in your iteration, but only from the current element, if you understand, what I mean.
So, you need to replace just the last line in your code:
# Mind the indentation
print(PT_list[num][0], round(PT_list[num][1], 2), PT_list[num][2])
Since you're storing data in your dictionary as a number mapped to a tuple (consisting of 3 elements), you can just access the requested data using indicies. Keep in mind, you need to respect the order of how you store the data in the tuple / dict.
Therefore:
PT_list[num][0] -> total
PT_list[num][1] -> average
PT_list[num][2] -> diagnosis
Solution 2:[2]
This method should also work:
for tot, avg, diag in PT_list:
print(tot, round(avg, 2), diag)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | nTerior |
| Solution 2 | BeRT2me |
