Python Forum
Using for loop in Python lists script? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Using for loop in Python lists script? (/thread-15113.html)



Using for loop in Python lists script? - NLittle17 - Jan-04-2019

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]


RE: Using for loop in Python lists script? - ichabod801 - Jan-04-2019

What have you tried? We're not going to write it for you. You will need a for loop and an if/else statement.


RE: Using for loop in Python lists script? - perfringo - Jan-04-2019

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.


RE: Using for loop in Python lists script? - NLittle17 - Jan-05-2019

(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'


RE: Using for loop in Python lists script? - ichabod801 - Jan-05-2019

You need the for loop (for score in scores:), and then do the conditional on score.