Reputation: 3
I am doing an exercise with python for a course I am currently enrolled in, but I cannot figure out how to add multiple inputs from the same loop together, here is the code I have so far:
ClassesTaken = input ("How many Classes are you taking?")
Class_Amount = int(ClassesTaken)
for i in range (Class_Amount):
print("Class", i+1)
Credits = int(input("How many credits for this class?"))
total = 0
total += Credits
print(total)
I am trying to add the inputs within the for loop
Upvotes: 0
Views: 941
Reputation: 4481
You need to move total = 0
outside your for
loop, as this is re-assigning a value of 0
on every iteration. Thus, you are currently only printing the last number of credits entered by the user.
Your code should therefore look like:
ClassesTaken = input ("How many Classes are you taking?")
Class_Amount = int(ClassesTaken)
total = 0
for i in range (Class_Amount):
print("Class", i+1)
Credits = int(input("How many credits for this class?"))
total += Credits
print(total)
Example input and output:
How many Classes are you taking?2
Class 1
How many credits for this class?10
Class 2
How many credits for this class?12
22
Upvotes: 2