Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to work with list
#9
This is not meant to entirely answer your question, but rather to offer another option in the approach - more proof of concept.
When working with a set of related data, a Python class may be a useful construct. Dictionaries work great for paired data, but in this case you have a student name, rank, math and history scores.

The following defines a class that has those elements as well as a function that sums the math and history score. Recognize that there is no error checking for someone entering a score that is not an integer, etc, but again this is more showing the concept. I also have it just take 2 student entries. I get the rank by order of entry, which may not work but again, doing this to show another approach.

You access data and functions within a class using the dot (.)

class student:
    name = ''
    place = ''
    math = ''
    history = ''

    def total(self):
        return int(self.math)+int(self.history)

students = [] # List of the students, will fill with student objects

for count in range(2): # Loop to enter the student names and scores
    studlet = student()
    studlet.name = input('Enter Name ')
    studlet.place = count
    studlet.math = input('Enter Math Score ')
    studlet.history = input('Enter History Score ')
    students.append(studlet) # Append the student object to the list of students

for student in students: # Loop to display the student object data
    print(student.name, student.place, student.math, student.history, student.total())
Output:
Enter Name John Enter Math Score 42 Enter History Score 38 Enter Name Mary Enter Math Score 45 Enter History Score 30 John 0 42 38 80 Mary 1 45 30 75
rob101 likes this post
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,355 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Beginner: Code not work when longer list raiviscoding 2 829 May-19-2023, 11:19 AM
Last Post: deanhystad
  how does .join work with list and dictionaries gr3yali3n 7 3,330 Jul-07-2020, 09:36 PM
Last Post: bowlofred
  A strange list, how does this work? Pedroski55 1 1,724 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,311 Jul-22-2019, 08:25 PM
Last Post: umut3806
  Example of list comprehensions doesn't work Truman 17 10,890 May-20-2018, 05:54 AM
Last Post: buran
  List 3 dimensions reference does not work Mario793 1 2,655 Mar-02-2018, 12:35 AM
Last Post: ka06059
  Trying to figure out how list comprehensions work tozqo 4 4,028 Jul-11-2017, 01:26 PM
Last Post: ichabod801
  Why list(dict.keys()) does not work? landlord1984 5 13,674 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