Jan-12-2023, 05:12 PM
(This post was last modified: Jan-17-2023, 06:52 PM by deanhystad.)
Allocate the points when the names are entered. Don't put them in a list.
You are starting out with the wrong structure. You should use a dictionary to keep track of student scores. The dictionay keys would be the student names. The student scores would be the dictionary values. With a dictionary you can look up the student's scores by using their name.
The same solution implemented using lists. See how much better a dictionary works?
You are starting out with the wrong structure. You should use a dictionary to keep track of student scores. The dictionay keys would be the student names. The student scores would be the dictionary values. With a dictionary you can look up the student's scores by using their name.
students = {} while True: if name := input('Enter student name (or blank to end list): '): students[name] = [0, 0] else: break for student, scores in students.items(): print(f'{student:20} {scores[0]:5} {scores[1]:5}')
Output:Enter student name (or blank to end list): A
Enter student name (or blank to end list): B
Enter student name (or blank to end list): C
Enter student name (or blank to end list):
A 0 0
B 0 0
C 0 0
Now you have a structure that has the student name associated with their scores (currently zero's). Next we need to enter their scores. Instead of making a list, ask for the student with the highest score, get that student from the dictionary and add the score to the student's scores. Math scores will be scores[0].students = {} while True: if name := input('Enter student name (or blank to end list): '): students[name] = [0, 0] else: break suffix = {1: 'st', 2: 'nd', 3:'rd'} score = 100 for i in range(1, len(students)+1): name = input(f'Student with {i}{suffix.get(i, "th")} score in Math: ') students[name][0] = score score -= 5 for student, scores in students.items(): print(f'{student:20} {scores[0]:5} {scores[1]:5}')
Output:Enter student name (or blank to end list): A
Enter student name (or blank to end list): B
Enter student name (or blank to end list): C
Enter student name (or blank to end list): D
Enter student name (or blank to end list):
Student with 1st score in Math: A
Student with 2nd score in Math: B
Student with 3rd score in Math: C
Student with 4th score in Math: D
A 100 0
B 95 0
C 90 0
D 85 0
Now you could repeat the same code for entering the History scores. And you could add a score to the students scores and copy the block of code to add a Language score and do the same to add an Arts score and so on. Or you could identify early on that there is a repeated pattern that can be exploited to eliminate duplicate code and make the program easier to modify.classes = {'Math': 0, 'History': 1} students = {} while True: if name := input('Enter student name (or blank to end list): '): students[name] = [0] * len(classes) else: break suffix = {1: 'st', 2: 'nd', 3:'rd'} for class_name, class_index in classes.items(): score = 100 for i in range(1, len(students)+1): name = input(f'Student with {i}{suffix.get(i, "th")} score in {class_name}: ') students[name][class_index] = score score -= 5 for student, scores in students.items(): print(f'{student:20}', *[f'{s:5}' for s in scores])
Output:Enter student name (or blank to end list): A
Enter student name (or blank to end list): B
Enter student name (or blank to end list):
Student with 1st score in Math: A
Student with 2nd score in Math: B
Student with 1st score in History: B
Student with 2nd score in History: A
A 100 95
B 95 100
The same solution implemented using lists. See how much better a dictionary works?
count = 3 names=[''] * count history=[0] * count maths=[0] * count for i in range(0, count): names[i] = input("Please enter the name of competitor ") suffix = {0: 'st', 1: 'nd', 2: 'rd'} score = 100 for i in range(0, count): name = input(f'Who came {i+1}{suffix.get(i)} in History Quiz: ') history[names.index(name)] = score score -= 5 score = 100 for i in range(0, count): name = input(f'Who came {i+1}{suffix.get(i)} in Math Quiz: ') maths[names.index(name)] = score score -= 5 for i in range(count): print(names[i], history[i], maths[i])
Output:Please enter the name of competitor A
Please enter the name of competitor B
Please enter the name of competitor C
Who came 1st in History Quiz: A
Who came 2nd in History Quiz: B
Who came 3rd in History Quiz: C
Who came 1st in Math Quiz: C
Who came 2nd in Math Quiz: B
Who came 3rd in Math Quiz: A
A 100 90
B 95 95
C 90 100