Python Forum
Nested loops, lists and if statements
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested loops, lists and if statements
#1
Hey guys I am having a real hard time understanding this question and would just appreciated some clarification on what the instructions are actually asking me to do. I know how to do lists and have a basic understanding of nested loops but executing it is another story

I have to make two class lists, one with Maths students and the other with Computer Science students. 

mathStudents = ['Audrey', 'Ben', 'Julia', 'Paul', 'Gerry', 'Sue', 'Helena', 'Harry', 'Marco', 'Rachel', 'Tina', 'Mark', 'Jackson'] 

csStudents = ['William', 'Aroha', 'Melissa', 'Sue', 'Ben', 'Audrey', 'Susan', 'Mark', 'Hemi', 'Brendan', 'Paul', 'Barry', 'Julia'] 

 I need to use these lists in nested loops to find out which students are enrolled in both classes and count the total number of students. 

My output should look like the following: 


Student: Audrey is enrolled in both classes 
Student: Ben is enrolled in both classes 
Student: Julia is enrolled in both classes 
Student: Paul is enrolled in both classes 
Student: Sue is enrolled in both classes 
Student: Mark is enrolled in both classes 

6  students are enrolled in Computer Science and Maths
Reply
#2
What have you tried? We prefer helping you fix code you have written to writing code for you.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Here's what you need to do:
1. Make a new list called commonClasses = []
2. Loop trough mathStudents only (because mathStudents and csStudents have the same length).
3. inside the loop, check if each student exists in csStudents by using in keyword.
4. Loop trough commonClasses and print the names in required form.

User has been warned for this post. Reason: Do not provide answers to those who haven't shown effort.
Reply
#4
(Apr-22-2017, 11:26 AM)idontreallywolf Wrote: if you wish to CHEAT:
Speaking as a moderator: please don't do people's homework for them. It's ok to give really heavy hints, but what you posted is far too much. I'd remove it, except that the the OP has Liked it so that would be pointless here.
Reply
#5
(Apr-22-2017, 09:22 PM)micseydel Wrote:
(Apr-22-2017, 11:26 AM)idontreallywolf Wrote: if you wish to CHEAT:
Speaking as a moderator: please don't do people's homework for them. It's ok to give really heavy hints, but what you posted is far too much. I'd remove it, except that the the OP has Liked it so that would be pointless here.

Yeah I got that.
I just needed a clarification on the instructions that were a bit more clearer but I do appreciate the help nonetheless.
Reply
#6
Not sharing my solution but will include sample code related to my solution.
For reference, I used : Understanding nested list comprehension syntax in Python
Reply
#7
Hey man I appreciate the help. much clearer now and I'll deff use that reference in future :)
Reply
#8
(Apr-23-2017, 04:50 AM)Liquid_Ocelot Wrote: Hey man I appreciate the help. much clearer now and I'll deff use that reference in future :)

No problem man! Lets learn together!
Reply
#9
The moderator just told me not to post code. You should delete it.
Reply
#10
I took my code down it doesn't help that you also have a copy of it quoted.
Even though I don't think the code provided is doing the work for him per say.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with nested loops robert5502001 7 3,571 Aug-01-2022, 06:26 AM
Last Post: stevensanders
  computing average in nested loops cap510 5 5,148 Sep-11-2020, 12:33 PM
Last Post: deanhystad
  Sorting nested lists in ascending order jszum 2 2,249 May-17-2020, 01:35 PM
Last Post: jefsummers
  loops in combination with lists Juru 4 2,811 May-07-2020, 02:58 PM
Last Post: Marbelous
  Capitalize first letter of names in nested loops student_of_python 9 4,697 Oct-27-2019, 07:51 AM
Last Post: Larz60+
  Recursions with nested lists sashiessay 2 2,772 Oct-05-2019, 11:40 AM
Last Post: sashiessay
  nested for loops to recursion ashkea26 4 3,485 Nov-02-2018, 05:00 PM
Last Post: ichabod801
  Please Help with lists and loops EthanA 3 2,487 Oct-27-2018, 08:24 AM
Last Post: wavic
  having an issue with nested if statements fad3r 6 4,646 May-08-2018, 12:25 AM
Last Post: ljmetzger
  Nested loops in openCV JimmyJangle 1 4,816 Apr-17-2018, 04:10 AM
Last Post: Mekire

Forum Jump:

User Panel Messages

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