Python Iterating over a list
Question:
Assign sum_extra
with the total extra credit received given list test_grades
. Full credit is 100, so anything over 100 is extra credit. For the given program, sum_extra
is 8 because 1 + 0 + 7 + 0 is 8. Sample output for the given program:
Sum extra: 8
Forgive me I am a starter on coding and really bad at it!
And this is my code (that is not working) Please help!:
test_grades = [101, 83, 107, 90]
sum_extra = -999 # Initialize 0 before your loop
while test_grades > 100:
test_grades = test_grades - 100
sum_extra = test_grades[0:3]
for test_grades[0:3] > 100:
test_grades = test_grades - 100
sum_extra= test_grade[0] + test_grades[1] + test_grades[2] + test_grades[3]
print('Sum extra:', sum_extra)
Which of my two loops would be better for this question and how can i edit them so that they become better? Because i can tell they are not going to work! any help is wanted! thank you! 🙂
Answers:
You could do
test_grades = [101, 83, 107, 90]
count = 0
for i in test_grades:
if i-100>0:
count += (i-100)
print count``
which is quite similar to your for loop except for the count
Your first loop will fail because test_grades
is of type list
, so it would not make sense to say while list < 100
.
an alternative while loop would look like:
i = 0;
while i < len(test_grades):
if test_grades[i] > 100:
test_grades[i] -= 100
else:
test_grades[i] = 0
print sum(test_grades)
Your second loop once again will fail because test_grades[0:3]
is once again of type list
, and you cannot have for list < 100
.
See L. M’s answer for an alternative.
A for
loop would be better for this type of question because you are iterating over elements, there is no real ‘condition’ to check, which would make a while loop better stylistically (they result in the same output).
A ‘pythonic’ approach would be to use a list comprehension:
test_grades = [101, 83, 107, 90]
extra_grades = [grade - 100 for grade in test_grades]
print sum(filter(lambda x: x > 0, extra_grades)) #prints 8
You should understand about the datatypes first.
In your code you are trying to compare a list to an integer, Which is not possible.
Also test_grades[0:3] will do the slicing, which will give you a sublist. So when you do this:
sum_extra = test_grades[0:3]
sum_extra becomes a list now. I feel you wanted it to remain and integer.
Now consider this line:
test_grades = test_grades - 100
This line is simply subtracting 100 from the current element of the list. So let’s say that the current element is 83. What should be your answer in this case ?
83-100 = -17 ?
Probably not. So you should first check whether the number is larger than 100, if yes then subtract 100 from it. Something like this:
if test_grages[i] > 100:
sum_extra = test_grades[i] -100 # Dont alter the content of test_grades. You may need to use it further in you code.
And you need to loop the above lines of code for every element of the list. So use a loop:
i=0 # First element is at 0th index.
while i < len(test_grades):
if test_grages[i] > 100:
sum_extra += test_grades[i] -100
i++
Now put this all-together:
test_grades = [101, 83, 107, 90]
sum_extra =0
i=0
while i < len(test_grades): #len() method gives you the length.
if test_grages[i] > 100:
sum_extra += test_grades[i] -100
i++
print(sum_extra)
Simply you can do
test_grades = [101, 83, 107, 90]
sum_extra = 0 # Initialize 0 before your loop
#iterate over the values of the list
for i in test_grades:
if i>100:
#add extra to the sum extra
sum_extra=sum_extra+(i-100)
#finally print the extra sum
print('Sum extra:', sum_extra)
user_input = input()
test_grades = list(map(int, user_input.split())) # contains test scores
sum_extra = -999 # Initialize 0 before your loop
sum_extra = 0
count = 0
for num in test_grades:
if num > 100:
sum_extra += num
count += 1
sum_extra = sum_extra - (100 * count)
print('Sum extra:', sum_extra)
user_input = input()
test_grades = list(map(int, user_input.split()))
sum_extra = 0
for num in test_grades:
if num >100:
sum_extra+=(num-100)
print('Sum extra:', sum_extra)
Assign sum_extra
with the total extra credit received given list test_grades
. Full credit is 100, so anything over 100 is extra credit. For the given program, sum_extra
is 8 because 1 + 0 + 7 + 0 is 8. Sample output for the given program:
Sum extra: 8
Forgive me I am a starter on coding and really bad at it!
And this is my code (that is not working) Please help!:
test_grades = [101, 83, 107, 90]
sum_extra = -999 # Initialize 0 before your loop
while test_grades > 100:
test_grades = test_grades - 100
sum_extra = test_grades[0:3]
for test_grades[0:3] > 100:
test_grades = test_grades - 100
sum_extra= test_grade[0] + test_grades[1] + test_grades[2] + test_grades[3]
print('Sum extra:', sum_extra)
Which of my two loops would be better for this question and how can i edit them so that they become better? Because i can tell they are not going to work! any help is wanted! thank you! 🙂
You could do
test_grades = [101, 83, 107, 90]
count = 0
for i in test_grades:
if i-100>0:
count += (i-100)
print count``
which is quite similar to your for loop except for the count
Your first loop will fail because test_grades
is of type list
, so it would not make sense to say while list < 100
.
an alternative while loop would look like:
i = 0;
while i < len(test_grades):
if test_grades[i] > 100:
test_grades[i] -= 100
else:
test_grades[i] = 0
print sum(test_grades)
Your second loop once again will fail because test_grades[0:3]
is once again of type list
, and you cannot have for list < 100
.
See L. M’s answer for an alternative.
A for
loop would be better for this type of question because you are iterating over elements, there is no real ‘condition’ to check, which would make a while loop better stylistically (they result in the same output).
A ‘pythonic’ approach would be to use a list comprehension:
test_grades = [101, 83, 107, 90]
extra_grades = [grade - 100 for grade in test_grades]
print sum(filter(lambda x: x > 0, extra_grades)) #prints 8
You should understand about the datatypes first.
In your code you are trying to compare a list to an integer, Which is not possible.
Also test_grades[0:3] will do the slicing, which will give you a sublist. So when you do this:
sum_extra = test_grades[0:3]
sum_extra becomes a list now. I feel you wanted it to remain and integer.
Now consider this line:
test_grades = test_grades - 100
This line is simply subtracting 100 from the current element of the list. So let’s say that the current element is 83. What should be your answer in this case ?
83-100 = -17 ?
Probably not. So you should first check whether the number is larger than 100, if yes then subtract 100 from it. Something like this:
if test_grages[i] > 100:
sum_extra = test_grades[i] -100 # Dont alter the content of test_grades. You may need to use it further in you code.
And you need to loop the above lines of code for every element of the list. So use a loop:
i=0 # First element is at 0th index.
while i < len(test_grades):
if test_grages[i] > 100:
sum_extra += test_grades[i] -100
i++
Now put this all-together:
test_grades = [101, 83, 107, 90]
sum_extra =0
i=0
while i < len(test_grades): #len() method gives you the length.
if test_grages[i] > 100:
sum_extra += test_grades[i] -100
i++
print(sum_extra)
Simply you can do
test_grades = [101, 83, 107, 90]
sum_extra = 0 # Initialize 0 before your loop
#iterate over the values of the list
for i in test_grades:
if i>100:
#add extra to the sum extra
sum_extra=sum_extra+(i-100)
#finally print the extra sum
print('Sum extra:', sum_extra)
user_input = input()
test_grades = list(map(int, user_input.split())) # contains test scores
sum_extra = -999 # Initialize 0 before your loop
sum_extra = 0
count = 0
for num in test_grades:
if num > 100:
sum_extra += num
count += 1
sum_extra = sum_extra - (100 * count)
print('Sum extra:', sum_extra)
user_input = input()
test_grades = list(map(int, user_input.split()))
sum_extra = 0
for num in test_grades:
if num >100:
sum_extra+=(num-100)
print('Sum extra:', sum_extra)