Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to work with list
#3
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.
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
Reply


Messages In This Thread
How to work with list - by kafka_trial - Jan-12-2023, 11:24 AM
RE: How to work with list - by rob101 - Jan-12-2023, 03:22 PM
RE: How to work with list - by kafka_trial - Jan-16-2023, 11:39 AM
RE: How to work with list - by rob101 - Jan-17-2023, 05:25 PM
RE: How to work with list - by kafka_trial - Jan-23-2023, 04:21 PM
RE: How to work with list - by deanhystad - Jan-23-2023, 04:34 PM
RE: How to work with list - by deanhystad - Jan-12-2023, 05:12 PM
RE: How to work with list - by rob101 - Jan-23-2023, 04:31 PM
RE: How to work with list - by jefsummers - Jan-24-2023, 01:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,399 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Beginner: Code not work when longer list raiviscoding 2 857 May-19-2023, 11:19 AM
Last Post: deanhystad
  how does .join work with list and dictionaries gr3yali3n 7 3,361 Jul-07-2020, 09:36 PM
Last Post: bowlofred
  A strange list, how does this work? Pedroski55 1 1,742 May-13-2020, 11:24 PM
Last Post: snippsat
Question Why does modifying a list in a for loop not seem to work? umut3806 2 2,328 Jul-22-2019, 08:25 PM
Last Post: umut3806
  Example of list comprehensions doesn't work Truman 17 10,961 May-20-2018, 05:54 AM
Last Post: buran
  List 3 dimensions reference does not work Mario793 1 2,669 Mar-02-2018, 12:35 AM
Last Post: ka06059
  Trying to figure out how list comprehensions work tozqo 4 4,057 Jul-11-2017, 01:26 PM
Last Post: ichabod801
  Why list(dict.keys()) does not work? landlord1984 5 13,724 Feb-02-2017, 05:52 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020