Python Forum
Using for loop in Python lists script?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using for loop in Python lists script?
#1
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]
Reply
#2
What have you tried? We're not going to write it for you. You will need a for loop and an if/else statement.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
(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'
Reply
#5
You need the for loop (for score in scores:), and then do the conditional on score.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Button to stop while loop from another script Absolutewind 5 819 Sep-25-2023, 11:20 PM
Last Post: deanhystad
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,009 Jun-29-2023, 11:57 AM
Last Post: gologica
  [SOLVED] [Linux] Script in cron stops after first run in loop Winfried 2 894 Nov-16-2022, 07:58 PM
Last Post: Winfried
  how to loop a script? ZYSIA 1 1,967 Jul-22-2021, 06:46 AM
Last Post: Gribouillis
Question How to print multiple elements from multiple lists in a FOR loop? Gilush 6 2,845 Dec-02-2020, 07:50 AM
Last Post: Gilush
Star --- Python lists and Linked Lists --- sabe 3 2,648 Nov-22-2020, 05:51 PM
Last Post: DeaD_EyE
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,312 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  How to kill a bash script running as root from a python script? jc_lafleur 4 5,791 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,248 May-28-2020, 05:27 PM
Last Post: micseydel
  for loop script over telnet in Python 3.5 is not working abhijithd123 1 2,860 May-10-2020, 03:22 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020