- when open a file, better use
with
context anager
with open('python_ist_cool_tabelle.txt', 'r') as f: # work with file f
- This file is short and there is no problem to read it in memory with
readlines()
, but you should know that you can iterate over f
for line in f:
- always use descriptive names.
a
is not such name.new_list
is better as e.g.students_grades
, names_list - justnames
,i
andk
is better asline
orstudent
- you can use extended iterable unpacking
name, *grades = line.split()
- don't use built-in functions, modules, etc. as names. In this case
sum
overrides the built-in function with same name.
- No need to have separate lists. if you are going to use data further, you can have a better data structure - e.g. list of dicts or list of custom Student class, etc.
- if you are going to just print average grade - you can do without any collection
- when iterate over list, etc. you iteterate over elements, no need to use indexes. when you are iterating over multiple iterables simultaneously, use
zip()
for name, average in zip(name, grade_averages):
- when print, try to use string formatting - e.g. f-strings or str.format() method. Concatenation works but you have to convert everything to
str
.
- you can make your code better if you split in small chunks and define functions. this way the code is reusable, easy to test, etc. For example you may have a function that will parse the line, and return name and grades converted to int. Next step would be to make use of class/OOP - e.g. Student class.
with open('test.txt', 'r') as f: students = [] # only if you have to keep data for processing later for line in f: name, *grades = line.split() # line has new-line character at the end, but in this case we don't need to strip it grades = [int(grade) for grade in grades] # convert grades to int print(f'{name} your average score is {sum(grades)/len(grades):.2f}') students.append({'name':name, 'grades':grades}) # add student info as dict # print students list print('---------------') print(students) print('---------------') for student in students: score = sum(student['grades'])/len(student['grades']) # calculate score on separate line print(f"{student['name']} your average score is {score:.2f}") # use calculated score in f-string
Output:joe your average score is 23.00
bill your average score is 20.00
sue your average score is 16.17
grace your average score is 23.67
john your average score is 35.20
---------------
[{'name': 'joe', 'grades': [10, 15, 20, 30, 40]}, {'name': 'bill', 'grades': [23, 16, 19, 22]}, {'name': 'sue', 'grades': [8, 22, 17, 14, 32, 17, 24, 21, 2, 9, 11, 17]}, {'name': 'grace', 'grades': [12, 28, 21, 45, 26, 10]}, {'name': 'john', 'grades': [14, 32, 25, 16, 89]}]
---------------
joe your average score is 23.00
bill your average score is 20.00
sue your average score is 16.17
grace your average score is 23.67
john your average score is 35.20
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs