0

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

2
  • 3
    Move total = 0 outside the for loop (above), since you re-assign it to 0 on every iteration. Commented Apr 7, 2020 at 4:00
  • It's recommended to use conventional variable names. These types of variables should all be lowercase with words separated by underscores. It makes the code easier to read, especially when you get deeper into the language. python.org/dev/peps/pep-0008/#function-and-variable-names Commented Apr 7, 2020 at 4:10

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

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.