|
| 1 | +# This program works with two CSV files. |
| 2 | +# One of them contains information about some students on a course: |
| 3 | + |
| 4 | +# id;first;last |
| 5 | +# 12345678;peter;pythons |
| 6 | +# 12345687;jean;javanese |
| 7 | +# 12345699;alice;adder |
| 8 | + |
| 9 | +# The other contains the number of exercises each student has completed each week: |
| 10 | + |
| 11 | +# id;e1;e2;e3;e4;e5;e6;e7 |
| 12 | +# 12345678;4;1;1;4;5;2;4 |
| 13 | +# 12345687;3;5;3;1;5;4;6 |
| 14 | +# 12345699;10;2;2;7;10;2;2 |
| 15 | + |
| 16 | +# As you can see above, both CSV files also have a header row, which tells you what each column contains. |
| 17 | + |
| 18 | +# Please write a program which asks the user for the names of these two files, reads the files, and then prints out the total number of exercises completed by each student. |
| 19 | +# If the files have the contents in the examples above, the program should print out the following: |
| 20 | + |
| 21 | + |
| 22 | +# Student information: students1.csv |
| 23 | +# Exercises completed: exercises1.csv |
| 24 | +# pekka peloton 21 |
| 25 | +# jaana javanainen 27 |
| 26 | +# liisa virtanen 35 |
| 27 | + |
| 28 | +# Hint: while testing your program, you may quickly run out of patience if you always have to type in the file names at the prompt. |
| 29 | +# You might want to hard-code the user input, like so: |
| 30 | + |
| 31 | +# if False: |
| 32 | +# # this is never executed |
| 33 | +# student_info = input("Student information: ") |
| 34 | +# exercise_data = input("Exercises completed: ") |
| 35 | +# else: |
| 36 | +# # hard-coded input |
| 37 | +# student_info = "students1.csv" |
| 38 | +# exercise_data = "exercises1.csv" |
| 39 | + |
| 40 | +# The actual functionality of the program is now "hidden" in the False branch of an if statement. It will never be executed. |
| 41 | + |
| 42 | +# Now, if you want to quickly verify the program works correctly also with user input, you can just replace False with True: |
| 43 | + |
| 44 | + |
| 45 | +# if True: |
| 46 | +# student_info = input("Student information: ") |
| 47 | +# exercise_data = input("Exercises completed: ") |
| 48 | +# else: |
| 49 | +# # now this is the False branch, and is never executed |
| 50 | +# student_info = "students1.csv" |
| 51 | +# exercise_data = "exercises1.csv" |
| 52 | + |
| 53 | +# When you have verified your program works correctly, you can remove the if structure, keeping the commands asking for input. |
| 54 | + |
| 55 | +student_info = input("Student information: ") |
| 56 | +exercise_data = input("Exercises completed: ") |
| 57 | + |
| 58 | +def create_dict(file): |
| 59 | + new_dict = {} |
| 60 | + with open(file) as new_file: |
| 61 | + for line in new_file: |
| 62 | + line = line.strip() |
| 63 | + parts = line.split(";") |
| 64 | + if parts[0] == "id": |
| 65 | + continue |
| 66 | + new_dict[parts[0]] = [] |
| 67 | + for item in parts[1:]: |
| 68 | + if item.isdigit(): |
| 69 | + new_dict[parts[0]].append(int(item)) |
| 70 | + else: |
| 71 | + new_dict[parts[0]].append(item) |
| 72 | + return new_dict |
| 73 | + |
| 74 | +def comb_dict(student_names, student_exercises): |
| 75 | + for id, name in student_names.items(): |
| 76 | + if id in student_exercises: |
| 77 | + print(f"{name[0]} {name[1]} {sum(student_exercises[id])}") |
| 78 | + |
| 79 | +student_names = create_dict(student_info) |
| 80 | +student_exercises = create_dict(exercise_data) |
| 81 | +comb_dict(student_names, student_exercises) |
0 commit comments