尝试使列表停止并在一定数量的条目后显示消息,但循环继续进行?



我正在完成一项作业,我们必须列出你计划在Python中学习的课程列表。列表中最多可以有5个类。当输入第六个类时,它应该要求您删除列表中已经存在的另一个类以添加下一个条目。下面是我的代码。当它运行时,它是一个无限循环,而不是在到达6时停止并显示消息。我能做些什么来解决这个问题?

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. ")

我已经合并了两个while循环在一起,并添加了一些if else
试试这个:

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)

即使您添加超过6个值,循环也不会停止的原因是,第一个'while'循环的条件要求它仅在您输入"exit"时停止。

这意味着你不会因为任何其他原因让第一个while循环结束。

要解决这个问题,你应该考虑你要做什么,我们最初的反应是"if the user tries to add more than 5 courses, he should be prompted to remove an existing course"。显然,这里的if条件是充分的。

但是我们意识到,如果用户试图删除不存在的课程(输入不在1到6之间),那么我们认为"while the 6th course is being added, keep prompting for removing a course, until successfully removed"。(这个问题可以用一种干净的方式来处理,这样我们直观的第一反应就可以在代码中看到,Read until the end)

读了上面的推理;

考虑以下内容:

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

现在,我还想补充一点,第一个while循环中的第二个while循环可能会显示为不干净。因此,您可能希望将其移动到函数中。

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. ")

使得代码变成:

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