Python Forum

Full Version: Loop through List
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Good morning, I can really use some helpm

I need to iterate over a list and if the criteria is met, add item to list. Then print list with the values. Here is what I have:
The code up until the end works as expected.

# After reading the data, 
# a. Print the state with the highest mean Verbal SAT score 
# b. Print each state that has a mean Math SAT score greater than 500



satfile = open('state_satscores_2004.txt','r')
count = 0
SATList = []

for line in satfile:
    count += 1
    textline = line.strip()
    items = textline.split()
    SATList.append(items)
        

# print the number of teams read
print('Number of Lines:', count)
print(SATList)
print('\n','\n')

state = []
for line in SATList:
     state.append(line[0])
print('Here is your state list')
print(state)
print('\n','\n')


verbal = []
for line in SATList:
     verbal.append(line[1])
print('Here is your verbal list')
print(verbal)
print('\n','\n')


math = []
for line in SATList:
     math.append(line[2])
print('Here is your Math list')
print(math)
print('\n','\n')


max_verbal = max(verbal)
max_state = state[verbal.index(max_verbal)]
print('The state with the highest mean Verbal SAT score is:',max_state,'With a score of:',max_verbal)
print('\n','\n')



#THIS IS THE PART I NEED HELP WITH
stategrades = []
goodgrades = []
print('Here are your good grades')
for i,j in SATList:
    if math[2] > str(500):
        stategrades.append(i)
        goodgrades.append(j)
print(stategrades)
Thank you for any help!!
More information would be helpful. What exactly is the problem you are having? If you get an error, please post the full text of the error. If the output is wrong, how is it wrong?

One thing I do note is line 59: comparing numbers with strings doesn't work well. '9' > '500' is True.
(Jul-30-2019, 12:51 PM)ichabod801 Wrote: [ -> ]More information would be helpful. What exactly is the problem you are having? If you get an error, please post the full text of the error. If the output is wrong, how is it wrong?

One thing I do note is line 59: comparing numbers with strings doesn't work well. '9' > '500' is True.


Thank you for responding. As of now I get a ValueError

IST_652_Quiz_1
ValueError                                Traceback (most recent call last)
<ipython-input-1-8b991c69553e> in <module>
     56 goodgrades = []
     57 print('Here are your good grades')
---> 58 for i,j in SATList:
     59     if math[2] > str(500):
     60         stategrades.append(i)

ValueError: too many values to unpack (expected 2)
Okay, SATList has three sub-items (state, verbal, math) in each item, and you are trying to unpack two. I'm not sure why you are comparing math[2] in the loop. That's the same value (the third math score in the file) for every time through the loop. I think you want:

for row in SATList:
    if row[2] > '500':
Although, I would again note that comparing numbers as strings does not work well. I would convert all of the scores with int() or float() as appropriate.
(Jul-30-2019, 03:17 PM)ichabod801 Wrote: [ -> ]Okay, SATList has three sub-items (state, verbal, math) in each item, and you are trying to unpack two. I'm not sure why you are comparing math[2] in the loop. That's the same value (the third math score in the file) for every time through the loop. I think you want:

for row in SATList:
    if row[2] > '500':
Although, I would again note that comparing numbers as strings does not work well. I would convert all of the scores with int() or float() as appropriate.

Thank you, I guess I'm not following. I thought I was unpacking all three suit items. Do you have a more accurate way to write the program.

I really appreciate the time and the lesson
(Jul-30-2019, 04:22 PM)jeran042 Wrote: [ -> ]I thought I was unpacking all three suit items.

No, you had for i,j in SATList:. i, j is only two names, so it only unpacks two values. To unpack three values you need three names on the left: i, j, k. Although it would be better to have more descriptive variable names.