0

So I have an otherwise easy homework assignment that wants me to input my grades for subjects, etc. The amount of input varies with the number of subjects the user wants to enter. The input works fine, but when I print out the saved values only the ones most recently entered are saved. Here is my code for input:

def gpa_input(classList):
    print("please enter how many classes you have")
    while True:
        try:
            numOfClasses = int(input())
        except ValueError:
            print("please enter a valid number")
            continue
        else:
            break
    for i in range (numOfClasses):
        classList.append(subject)
        print("enter name of " + str(i+1) + "th subject:")
        classList[i].name = input()
        print("enter num of credits for " + str(i+1) + "th subject:")
        while True:
            try:
                classList[i].credits = int(input())
            except ValueError:
                print("please enter a valid number")
                continue
            else:
                break
        print("enter grade for " + str(i+1) + "th subject:")
        while True:
            try:
                classList[i].gradePercentage = int(input())
            except ValueError:
                print("please enter a valid number")
                continue
            else:
                break

A subject is a class containing a string value and 2 int values, defined as following:

class subject:
    def __init__(name, credits, gradePercentage):
        self.name = name
        self.credits = credits
        self.gradePercentage = gradePercentage

And here is the code that prints out all of this:

def main():
  gpa_input(classList)
  for i in range (len(classList)):
      print(classList[i].name)
      print(classList[i].credits)
      print(classList[i].gradePercentage)

What is the problem with my code? Am I iterating through something the wrong way, or is there something not getting properly assigned/saved?

1 Answer 1

1

You've got into the very common "trap". The problem is in how you initialize your subject class.

Here you just append a class to the list: classList.append(subject)

The situation here is the following:
Once you called subject without braces you will have a new object.
But when you call it on the second time - python will not initialize the new object for you and just return the object created on the first step.
So all you need is to properly initialize all subject objects.

Two ways:
1) Remove args from subject definition and make default values are none + add braces to the classList.append(subject)
2) Collect all values in your for loop into variables and at the end of the function initialize you subject class with proper args.

Sign up to request clarification or add additional context in comments.

1 Comment

By "add braces" do you mean classList.append(subject())?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.