2

So I am completely new to coding and I'm going through a class right now and I'm stuck. Essentially the problem is this:

Assign sum_extra with the total extra credit received given list test_grades. Full credit is 100, so anything over 100 is extra credit.

Sample output for the given program with input: '101 83 107 90'

My current code is this:

user_input = input()
test_grades = list(map(int, user_input.split())) # test_grades is an integer list of test scores


sum_extra = 0 

for grade in test_grades: 
    if grade > 100:
        sum_extra = grade - 100
        
print('Sum extra:', sum_extra)

The problem I am having is that it isn't adding the total extra over 100. Like for the example inputs above it runs and totals 7 instead of 8. I'm honestly not sure what I am missing. Also if it isn't too much trouble, I would prefer to just get a nudge in the right direction rather than a flat out answer. Thank you so much.

5
  • 1
    You need to add to previous value of sum_extra. right now you are overwriting it. Commented Jun 10, 2021 at 2:39
  • Also, it is a good practice to give a prompt when asking user input. user_input = input("Enter the grades") Commented Jun 10, 2021 at 2:40
  • sum_extra+=(int(user_input)-100). This will add the value to sum_extra Commented Jun 10, 2021 at 2:43
  • Thank you for all the suggestions and help. It really helped out. Commented Jun 10, 2021 at 3:51
  • As an option: sum_extra = sum(x-100 for x in test_grades if x>100) Commented Jun 26, 2024 at 6:14

5 Answers 5

1
sum_extra = grade - 100

That line of code assigns a new value to sum_extra, discarding whatever value it already had.

Instead, you want to add to the existing value:

sum_extra = sum_extra + (grade - 100)
Sign up to request clarification or add additional context in comments.

Comments

0

sum_extra needs to contain the sum of all the extra points. As written your code overwrites it with the current excess each time rather than adding together all the extra points.

Comments

0

While iterating through the test_grades list, if grade is greater than 100 then you need to update the sum_extra i.e. sum_extra += grade - 100

So the exact code will be like this

user_input = input()
test_grades = list(map(int, user_input.split())) # test_grades is an integer list of test scores


sum_extra = 0 

for grade in test_grades: 
    if grade > 100:
        sum_extra += grade - 100
        
print('Sum extra:', sum_extra)

Comments

0

In the following statement, sum_extra = grade - 100, you are updating sum_extra value. To get the sum of the extra credits you need to accumulate by doing the following: sum_extra += (grade - 100).

1 Comment

Thanks for answering the question, but similar answer is already posted by someone. You should only post an answer if it is different from others' answers.
0
user_input = input()
test_grades = list(map(int, user_input.split())) # test_grades is an integer list of test scores

sum_extra = -999 # Initialize to -999 before your loop

sum_extra = 0 # Resetting sum_extra to 0 for calculation
for grade in test_grades:
    if grade > 100:
        sum_extra += (grade - 100)

print('Sum extra:', sum_extra)

1 Comment

I believe your answer could be improved with a word or two about the idea behind the code. For example, what's the point of sum_extra = -999? And how is it different from other answers already given?

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.