I need to make a table that pulls one or more person’s name and scores at various games from a text file, then find the average, minimum and maximum of each person’s scores and display them.
EDIT-here is my current code:
while name!='':
floor=text_file.readline().rstrip()
rings=text_file.readline().rstrip()
p_bars=text_file.readline().rstrip()
p_horse=text_file.readline().rstrip()
h_bar=text_file.readline().rstrip()
floor_int=int(floor)
rings_int=int(rings)
p_bars_int=int(p_bars)
p_horse_int=int(p_horse)
h_bar_int=int(h_bar)
score_sum=floor_int+rings_int+p_bars_int+p_horse_int+h_bar_int
avg=score_sum/5
how would I be able to do math with the lines that have numbers and exclude the lines that have names?
EDIT: running that code gives me this:
TypeError: Can’t convert ‘float’ object to str implicitly
any ideas?
Assuming ‘rings’ is a line that has a number, you can add all those by creating int or float objects, for example:
If there is a mix of numbers and other text within a line, use a regular expression to pull out the number part first:
I use a lot of individual variables to clarify this, but the steps can be combined, for example:
P.S. If you are getting that error, it is probably in a print statement where you are trying to print avg without converting it to a string somehow. Either of these works: