Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to work with list
#6
Thanks for that. Good stuff.

Just for my own sanity: why do I get the following message:

Error:
Traceback (most recent call last): File "C:/Users/mustafa.turus/OneDrive - Capital City College Group/Desktop/sirrrrr.py", line 24, in <module> total[i]=history[i]+maths[i] IndexError: list assignment index out of range
count = 3
names=[''] * count
history=[0] * count
maths=[0] * count
total=[]
 
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(0,count):
    total[i]=history[i]+maths[i]
print(total)
(Jan-17-2023, 05:25 PM)rob101 Wrote:
(Jan-16-2023, 11:39 AM)kafka_trial Wrote: This is very useful. Thank you.

Just one more question. How I would add the points for maths and history for each learners.

For example, let's say that Student John came fourth math which is 35 points and John came first in history which is 50 points. How could add these points.

Thanks.

PS: No, I don't know anything about dictionary yet. Sad But I will learn asap.

You're welcome.

A Python dictionary is a very useful data construct, for which your use case could be:

order = {"first": 100,
         "second": 95,
         "third": 90,
         "fourth": 85,
         "fifth": 80
         }
... and so on, but do you need that kind of thing? Why do you need that way of displaying the rankings?

I'd also like to better understand the points system, as it seems to be complicated. As an example, in your first post you said "... first place 100, second 95, third 90... so on.", but now you post "... let's say that Student John came fourth math which is 35 points and John came first in history which is 50 points."

A consistent scoring system would make the coding much simpler. For example, if there's always a 5 point separation between placements, then each place could be calculated by simply subtracting 5 points from 100 for each place. That would then make it quite easy to allocate points to each candidate, based on the position in the list:

# the data as entered, for example
math = ['Paul', 'Ringo', 'John', 'George']
history = ['George', 'Ringo', 'John', 'Paul']
totals = []

name = 1
score = 0

for position, names in enumerate(math):
    math[position] = 100 - 5 * position, names
math.sort(reverse=True)

for position, names in enumerate(history):
    history[position] = 100 - 5 * position, names
history.sort(reverse=True)

print("Results for math:")
for scores in math:
    print(f"{scores[name]}: {scores[score]}")

print()
print("Results for history:")
for scores in history:
    print(f"{scores[name]}: {scores[score]}")

for h_score in history:
    for m_score in math:
        if h_score[name] == m_score[name]:
            totals.append((h_score[score] + m_score[score], h_score[name]))
totals.sort(reverse=True)

print()
print("Totals:")
for scores in totals:
    print(f"{scores[name]}: {scores[score]}")
Output:
Results for math: Paul: 100 Ringo: 95 John: 90 George: 85 Results for history: George: 100 Ringo: 95 John: 90 Paul: 85 Totals: Ringo: 190 Paul: 185 George: 185 John: 180
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
  Extending list doesn't work as expected mmhmjanssen 1 45 1 hour ago
Last Post: deanhystad
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,405 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Beginner: Code not work when longer list raiviscoding 2 860 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,329 Jul-22-2019, 08:25 PM
Last Post: umut3806
  Example of list comprehensions doesn't work Truman 17 10,962 May-20-2018, 05:54 AM
Last Post: buran
  List 3 dimensions reference does not work Mario793 1 2,671 Mar-02-2018, 12:35 AM
Last Post: ka06059
  Trying to figure out how list comprehensions work tozqo 4 4,061 Jul-11-2017, 01:26 PM
Last Post: ichabod801
  Why list(dict.keys()) does not work? landlord1984 5 13,726 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