Home > Enterprise >  Checking a list to make sure a user inputted item is in there
Checking a list to make sure a user inputted item is in there

Time:11-05

condition = True
classes = []
print("Welcome to class registration!")
def printClasses():
    print("You are currently taking these courses: ")
    for item in range(0, len(classes)):
            print(item 1, ".", classes[item])

while condition:
    if(len(classes) < 5):
        course = str(input("What course(s) would you like to take?: \n"))
        course = course.title()
        course = str(course)
        print(course)
        print(len(classes))
        classes = course.split(",")
        for i in range(len(classes)):
            classes[i] = classes[i].strip()
        print(len(classes))
        print(classes)

        printClasses()
    elif (len(classes) > 5):
        removeClass = input("Please select a class to remove: \n")
        removeClass = removeClass.title()
        removeClass = str(removeClass)
        removeClass = removeClass.strip()
        classGone = []
        classGone = removeClass.split(",")
        for i in range(len(classGone)):
            classGone[i] = classGone[i].strip()
        for item in classGone:
            removeClass = []
            inputCheck = classGone.count(removeClass)
            if inputCheck > 0:
                classes.remove(item)
            else:
                 print("Please select a class that exists...")
                

        printClasses()

    else:
        print("Done!")
        break

Im having trouble with my inputCheck statement. I need to be able to remove things from the list but they have to be on there. Thank you!

I tried to make a iputCheck variable that checks the list for the input to make sure it matches something in the list, but it all went downhill.

CodePudding user response:

There were numerous mistakes in your code. Instead of pointing then all out I have fixed them and will allow you to figure it out on your own.

global classes
classes = []
    
print("Welcome to class registration!")
    
def printClasses():
    print("You are currently taking these courses: ")
    count = 1
    for item in classes:
        print('{}. {}'.format(count, item))
        count  = 1
    
while True:
    if(len(classes) < 5):
        course = str(input("What course(s) would you like to take?: \n"))
        classes  = course.split(",")
        for i in range(len(classes)):
            classes[i] = classes[i].strip().title()
        printClasses()
    elif (len(classes) > 5):
        removeClass = str(input("Please select a class to remove: \n"))
        classGone = removeClass.split(",")
        for item in classGone:
            i = str(item)
            if i.strip().title() in classes:
                classes.remove(i)
            else:
                print("Please select a class that exists...")
        printClasses()
    
    else:
        print("Done!")
        break

CodePudding user response:

Had a pretty hard time understanding what you meant but I think I got it. You want to check if the selected class for removal is in the list? If that's the case you can use the following code:

#The list
classes=["English","Math","Coding"]

##Asking what classes to remove
Class_To_Remove=input("What class do you want to remove?")

#Removing the classes and checking if they are
for x in classes:
    if Class_To_Remove.capitalize() in x:
        classes.remove(Class_To_Remove.capitalize())
        print("Class removed")
    else:
        print("Class isn't added")

If I understand everything correctly this will replace your:

    removeClass = input("Please select a class to remove: \n")
    removeClass = removeClass.title()
    removeClass = str(removeClass)
    removeClass = removeClass.strip()
    classGone = []
    classGone = removeClass.split(",")
    for i in range(len(classGone)):
        classGone[i] = classGone[i].strip()
    for item in classGone:
        removeClass = []
        inputCheck = classGone.count(removeClass)
        if inputCheck > 0:
            classes.remove(item)
        else:
             print("Please select a class that exists...")

For future reference try to only use the few lines you are having trouble with and explaining the problem more clear.

CodePudding user response:

I've been trying to understand this for some time. For the next one please be more clear. Since there's several mistakes on your code, I tried to make a solution that could work for you. Here it is!

global classes
classes = []

print("Welcome to class registration!")


def printClasses():
    print("You are currently taking these courses: ")
    for i in range(0, len(classes)):
        print(i 1, ".", classes[i])


control = True
while control:
    if(len(classes) < 5):
        course = str(input("What course(s) would you like to take?: \n"))
        classes  = course.split(",")
        for i in range(len(classes)):
            classes[i] = classes[i].strip().title()
        printClasses()
    elif (len(classes) >= 5):
        option = input("Do you want to remove a class?\n 1.Yes\n 2.No\n")
        if option == "1":
            classGone = []
            for i in range(1):
                removeClass = input("Please select a class to remove: \n")
                if (removeClass == classes[0] or
                    removeClass == classes[1] or
                    removeClass == classes[2] or
                    removeClass == classes[3] or
                        removeClass == classes[4]):
                    classes.remove(removeClass)
                    classGone.append(removeClass)
                    print("Classes removed are: ", classGone)
                    printClasses()
                    i  = 1
                    break
                else:
                    print("Please select a valid class")
        elif option == "2":
            print("Done!")
            break

Hope it works!

  • Related