Home > OS >  Trying to make a list stop and display a message after a certain amount of entries, but the loop kee
Trying to make a list stop and display a message after a certain amount of entries, but the loop kee

Time:10-26

I am completing an assignment where we have to make a list of classes you plan to take in Python. The max amount of classes you can have in the list is 5. When the sixth class is entered, it should ask you to drop another class in already in the list to add the next entry. I have my code so far below. When it runs, it is an endless loop, rather than stopping and displaying the message when it reaches 6. What can I do to fix this?

courseList = []

#sets beginning of loop to 0
course = 0
courses = "none"

#input to enter course name
while (courses != "Exit"):
    courses = str(input("What is the name of the course? "))
    courseList.append(courses)
    course = course   1
    if (courses == "Exit"):
        course = course - 1
#input to drop course
while (course >= 6):
    print(courseList)
    x = int(input("What course # will you drop? "))
    if(1<= x <=6):
        courseList.pop(x-1)
        course = course -1
        print(courseList)
    #error message
    else:
        print("Please select a # between 1-6. ")

CodePudding user response:

I have merged the two while loops together and added some if else
Try this:

CourseList = []

while True:
    # Input to enter course name.
    course = input("What is the name of the course?\n")
    # Check if user wants to exit.
    if course == "Exit":
        break
    # Check the length of the list. If greater than 5, the user selects soemthing to drop.
    elif len(CourseList) >= 5:
        CourseList.append(course)
        # Show user the courses in the list
        print(CourseList)
        x = input("What course # will you drop?\n")
        # Check if x is a number or in range, if not, let the user input again.
        while x.isdigit() == False or x < '1' or x > '6':
            print("Please choose a number between 1 and 6")
            x = input("What course # will you drop?\n")
        x = int(x)
        CourseList.pop(x - 1)
        print(CourseList)
    # If none of above, append the course
    else:
        CourseList.append(course)

CodePudding user response:

The reason the loop does not stop even if you add more than 6 values is that, the condition of the first 'while' loop requires it to stop only if you input "exit".

Which means really that you dont get the first while loop to end for any other reason.

To solve this issue, you should consider what youre trying to do, our initial response is "if the user tries to add more than 5 courses, he should be prompted to remove an existing course". And it becomes apparent that an if condition is sufficient here.

But then we realise, what if the user tries to remove course that does not exist (input is not between 1 and 6). So then we think "while the 6th course is being added, keep prompting for removing a course, until successfully removed". (This issue can be handled in a clean way, such that our intuitive first response is seen in the code, Read till the end)

Having read the reasoning above;

Consider the following:

courseList = []

#sets beginning of loop to 0
course = 0
courses = "none"

#input to enter course name
while (courses != "Exit"):
    courses = str(input("What is the name of the course? "))

    # Move this to the top, since if user is trying to exit the loop, nothing else needs to be done but that
    if (courses == "Exit"):
        break

### Add this ###
    while course >= 5:          # This is essentially also implying if course >= 5 if you think about it
        print(courseList)
        x = int(input("What course # will you drop? "))
        if 1 <= x <= 5:
            courseList.pop(x-1)
            course = course - 1
        else:
            print("Please select a # between 1-5. ")
### END ###

    courseList.append(courses)
    course = course   1

Now, I also want to add that the second while loop within the first may come off as not clean. For that reason you may want to move it into a function.

def removeCourse(lst, course):
    while course >= 5:
        print(courseList)
        x = int(input("What course # will you drop? "))
        if 1 <= x <= 5:
            courseList.pop(x-1)
            course = course - 1
        else:
            print("Please select a # between 1-5. ")

which then makes the code become:

while (courses != "Exit"):
    courses = str(input("What is the name of the course? "))

    if (courses == "Exit"):
        break

    if course >= 5:        # Note how we change this to the if condition now, which was our initial response to the problem, because the function handles the case where the input is not valid
        removeCourse(courseList, course)

    courseList.append(courses)
    course = course   1

  • Related