I'm a beginner with python and I'm trying to write a program that asks the user for how many classes they have taken, then asks them for the class codes and prints the classes they have left to take.

I can get the program to run perfectly for one list but the trick is there are 3 lists. I made listA the computer science courses which has to print with a header of "part A requirements" and then print each item in the list on a separate line.

The part I am stumped on is removing items from the other separate lists all from one user input, and then printing each list showing what requirements are left for each.

def main(): 
    print "This program returns a list of classes you still have to take: "
    classlistA = ["CMSC 201", "CMSC 202", "CMSC 203", "CMSC 304", "CMSC 313",
                 "CMSC 331", "CMSC 341", "CMSC 345", "CMSC 411", "CMSC 421",
                 "CMSC 441"]
    classlistB = ["MATH 151", "MATH 152", "MATH 221"]
    classlistC = ["STAT 334" ,"STAT 451"]
    classname = input ("How many classes have you taken so far?: ")
    
    for i in range(classname):
        classname = raw_input("Please type the classes you have taken <CODE><COURSE-NUMBER>: ")
    classlistA.remove(classname)

    
    
    print "part A requirements"
    for x in classlistA:
        print "You still have to take", x


main()

There are numerous solutions to this problem, but perhaps one that is most straightforward is to just put all your items in a list of lists. Or a tuple of lists, or a dictionary of lists...etc there's many advanced storage options in python.

Here's a list of lists:

def main(): 
    print "This program returns a list of classes you still have to take: "
    classlistA = ["CMSC 201", "CMSC 202", "CMSC 203", "CMSC 304", "CMSC 313",
                 "CMSC 331", "CMSC 341", "CMSC 345", "CMSC 411", "CMSC 421",
                 "CMSC 441"]
    classlistB = ["MATH 151", "MATH 152", "MATH 221"]
    classlistC = ["STAT 334" ,"STAT 451"]

    all_lists=[ classlistA, classlistB, classlistC]

    classname = input ("How many classes have you taken so far?: ")
    
    for i in range(classname):
        classname = raw_input("Please type the classes you have taken <CODE><COURSE-NUMBER>: ")
    for classlists in all_lists:
        try classlists.remove(classname)
    except Exception:
        print 'cannot remove that, not found'
        pass
    
    
    print "part A requirements"
    for x in classlistA:
        print "You still have to take", x


main()

What I did was I just put your 3 classlists in a new list called all_lists. Then I also put in an error exception that will handle the situation when you try to remove an element that is not there.

Again, there are many more elegant solutions.

Ill mess around with that idea and see what I can do. Thanks!

I tried out a multiple ways of doing lists of lists and the one problem I'm running into now is when I go to print out the remaining items in each list. I have to print them in the format of

"part A requirements"
CMSC<number>...

"Part B requirements"
MATH<number>...

etc.

Because I am making a list of lists, I am only editing the big list, not the individual ones, so when I go to print out only the specific items for each part such as partA, partB, and partC, I still print out every item.

Is there a way to separate out each newly edited list from the bigger allLists so that I can print each part separately? Here is my new code so far

print "This program returns a list of classes you still have to take: "
    classlistA = ["CMSC 201", "CMSC 202", "CMSC 203", "CMSC 304", "CMSC 313",
                  "CMSC 331", "CMSC 341", "CMSC 345", "CMSC 411", "CMSC 421",
                  "CMSC 441"]
    classlistB = ["MATH 151", "MATH 152", "MATH 221"]
    classlistC = ["STAT 334" ,"STAT 451"]
     
    allLists = (classlistA + classlistB + classlistC)
 
     
    classnumber = input ("How many classes have you taken so far?: ")
     
    for i in range(classnumber):
        classname = raw_input("Please type the classes you have taken <CODE> <COURSE-NUMBER>: ")
        allLists.remove(classname)
    
    
    print "part A requirements"
    for x in classlistA: ## will print out everything in classlistA without removing classes
        print "You still have to take", x

         
    print "part B requirements"
    for classlistB in allLists:## will print out everything allLists with classes removed
        print "You still have to take", classlistB
         
  ##  print "part C requirements"
  ##  for x in classListsC:
   ##     print "You still have to take", x

Hmm, so the way you defined alllists, you did not make a list of lists; rather, you simply made a single list by adding all the items from your smaller lists together. To make a list of lists, you'd use this syntax:

alllists=[classlistA, classlistB, classlistC ]

Then I'd change the following line:

for i in range(classnumber):
       classname = raw_input("Please type the classes you have taken <CODE> <COURSE-NUMBER>: ")
       allLists.remove(classname)

To

for i in range(classnumber):
       classname = raw_input("Please type the classes you have taken <CODE> <COURSE-NUMBER>: ")
       for classlist in alllists:
           if classname in classlist:
               classlist.remove(classname)

The simplest is to use a dictionary

classes_taken={classlistA:["CMSC 201", "CMSC 202", "CMSC 203"],
               classlistB:["MATH 151", "MATH 152"]}
etc.

If you want to use lists then you can just transfer the classes taken to the list, so you would have 3 classes_taken lists or one list with 3 sublists. You can also use one list in this way

print "This program returns a list of classes you still have to take: "
classlistA = ["CMSC 201", "CMSC 202", "CMSC 203", "CMSC 304", "CMSC 313",
              "CMSC 331", "CMSC 341", "CMSC 345", "CMSC 411", "CMSC 421",
              "CMSC 441"]
classlistB = ["MATH 151", "MATH 152", "MATH 221"]
classlistC = ["STAT 334" ,"STAT 451"]

# simulate this data was entered
test_data = ["CMSC 201", "CMSC 202", "STAT 334", "MATH 151", "MATH 152"]
set_test_data=set(test_data)

## print classes to be taken
letters = ["A", "B", "C"]
for ctr, classlist in enumerate([classlistA, classlistB, classlistC]):
    print "You have not taken the following classes in %s" % (letters[ctr])
    for this_class in classlist:
        if this_class not in test_data:
            print "     ", this_class

    print "\nEven easier with sets"
    print set(classlist).difference(set_test_data), "\n"

Thanks, I used your suggestions and got it working correctly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.