Jan-23-2023, 04:21 PM
Thanks for that. Good stuff.
Just for my own sanity: why do I get the following message:
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. But I will learn asap.
You're welcome.
A Pythondictionary
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