0

I'm trying to ask the user to input how many classes they have (x), ask "What are your grades in those classes?" x amount of times, and record all of the inputted grades to use later.

I tried to assign the question to a variable and ask to print the variable, but I get only the last inputted number. I don't want to print the numbers, I want to store them for later so I can add them together. I was just using the print function to see how my numbers would be stored if assigning the variable actually worked. How would I record all the inputted numbers to later add and calculate GPA?

numofclasses = int(input("How many honors classes do you have?: "))
for i in range(numofclasses):
  grades = str(input("Enter the unweighted grade from one class "))

print(grades)

I want to get all the inputted numbers recorded, but by using the print option I only get the last inputted number recorded.

3 Answers 3

2

The thing you want to use is a list, which is used to container which holds a sequence of datatypes, like integer, characters, etc,

Think of it this way, if you want to use 3 variables in python what would you generally do

a = 1
b = 2
c = 3

This works fine, but what if the number of variables is 50, or 100, how many variables will you keep defining, hence you would need a container to store these, which is where a list comes in. So we would just do

li = [1,2,3]

And access these variables via indexes, which start from 0

a[0] #1
a[1] #2
a[2] #3

Keeping this in mind, we would do!

numofclasses = int(input("How many honors classes do you have?: "))

#List to save all grades, defined by assigning variable to []
all_grades = []
for i in range(numofclasses):

    #Take grades from the user
    grades = input("Enter the unweighted grade from one class ")

    #Append the grades to the list, using list.append function
    all_grades.append(grades)

#Loop through the list to print it
for item in all_grades:
    print(item)

#Print all grades in a single line by joining all items of list in a string
s = " ".join(all_grades)
print(s)

And the output will look like

How many honors classes do you have?: 3
Enter the unweighted grade from one class A
Enter the unweighted grade from one class B
Enter the unweighted grade from one class C
#All grades in different lines
A
B
C
#All grades in single line
A B C
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for the help! Is there a way to add together the inputted grades (A, B, C)?
If by adding together letters you mean printing all of them in a single line @LilyHebert ? If yes ser my uofated answer
0

It seems to me there are a couple options that may be suitable.

Printing the input each iteration:

numofclasses = int(input("How many honors classes do you have?: "))
for i in range(numofclasses):
    grades = str(input("Enter the unweighted grade from one class "))
    print(grades) # move print to inside of loop

Storing the values in a list for printing later:

numofclasses = int(input("How many honors classes do you have?: "))
grades = []
for i in range(numofclasses):
    grades.append(str(input("Enter the unweighted grade from one class ")))

print(grades) # will look like ["A", "B", "C", "B"]

2 Comments

Thank you very much for the helpful response! If I were to use the second option, is there a way for me to add together all the inputted grades (A, B, C, D)?
Yes, something like print(“, ”.join(grades)) (untested since I’m on mobile, but should be free of typos) the “, “ part says what you want put between items. It can be any string, or even “” (empty string) which would yield ABCD instead of A, B, C, D
0

Here is how yo do it:

class_dict = {}
numOfClasses = input("How many classes do you take? Enter here : ")
for i in range(int(numOfClasses)):
    class_dict["class" + str(i +1)] = input("Enter your grade for class " + str(i +1) + ":  ")

print(class_dict)

The above should do it.

Comments

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.