Python Forum

Full Version: Using for loop in Python lists script?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
If you have a list like below and now need to print a message for grades over 65 saying “You passed!” and another message saying “Sorry, you failed.” for grades below 65 how would you put that into the script? I think it should be written with a for loop but I’m stuck.

grades = [57, 98, 100,55, 60, 62, 54, 88, 92, 97, 74]
What have you tried? We're not going to write it for you. You will need a for loop and an if/else statement.
One way is to articulate your task in english, something like "compare every grade in grades with 65, if grade is larger then print something, else print something else" and then write Python code based on that.
(Jan-04-2019, 10:04 PM)NLittle17 Wrote: [ -> ]If you have a list like below and now need to print a message for grades over 65 saying “You passed!” and another message saying “Sorry, you failed.” for grades below 65 how would you put that into the script? I think it should be written with a for loop but I’m stuck. grades = [57, 98, 100,55, 60, 62, 54, 88, 92, 97, 74]

What I have written is:

if scores >= 65:
print("Passing")

else:
print("Not Passing")

Error:
Traceback (most recent call last):
File "python", line 19, in <module>
TypeError '>=' not supported between instances of 'list' and 'int'
You need the for loop (for score in scores:), and then do the conditional on score.