这是制作课程注册程序的硬件问题。我觉得我已经完成了大部分工作,但是我很难理解为什么在第二次while循环中,如果将choice值设置回A不会返回到添加课程循环。
choice = input("Enter A to add course, D to drop course, and E to exit: ")
choice = choice.upper()
courses = []
while choice == "A":
course1 = input("Enter a course to add: ")
courses.append(course1)
courses.sort()
print("Courses Registered: ", courses)
choice = input("Enter A to add course, D to drop course, and E to exit: ")
choice= choice.upper()
else:
while choice == "D":
drop1 = input("Enter a course to drop: ")
if drop1 in courses:
courses.remove(drop1)
print("Course Dropped")
courses.sort()
print("Courses Registered: ", courses)
choice = input("Enter A to add course, D to drop course, and E to exit: ")
choice = choice.upper()
else:
print("You are not registered in that class")
choice = input("Enter A to add course, D to drop course, and E to exit: ")
choice = choice.upper()
else:
if choice == "E":
print("You have exited the program")
else:
print("Invalid input")
choice = input("Enter A to add course, D to drop course, and E to exit: ")
choice = choice.upper()
直到我放弃一堂课并想回到添加课之前,它一直有效。然后,它将继续在代码中继续,并将显示“无效输入”
答案 0 :(得分:0)
使用break
语句终止while loop
,否则继续更新选择
courses = []
choice = input("Enter A to add course, D to drop course, and E to exit: ")
choice = choice.upper()
while True:
if choice == "A":
course1 = input("Enter a course to add: ")
courses.append(course1)
courses.sort()
print("Courses Registered: ", courses)
choice = input("Enter A to add course, D to drop course, and E to exit: ")
choice= choice.upper()
elif choice == "D":
drop1 = input("Enter a course to drop: ")
if drop1 in courses:
courses.remove(drop1)
print("Course Dropped")
courses.sort()
print("Courses Registered: ", courses)
choice = input("Enter A to add course, D to drop course, and E to exit: ")
choice = choice.upper()
else:
print("You are not registered in that class")
choice = input("Enter A to add course, D to drop course, and E to exit: ")
choice = choice.upper()
elif choice == "E":
print("You have exited the program")
break
else:
print("Invalid input")
choice = input("Enter A to add course, D to drop course, and E to exit: ")
choice = choice.upper()
答案 1 :(得分:0)
我建议不要使用while / else构造。任何无效输入后,您的代码都会退出,不会重复。第二个循环仅接受D,而不接受A。因此,仅使用一个循环
尝试以下结构
courses = []
while True:
choice = input("Enter A to add course, D to drop course, and E to exit: ").upper()
if choice == "E":
break
elif choice == "A":
course1 = input("Enter a course to add: ")
courses.append(course1)
courses.sort()
print("Courses Registered: ", courses)
elif choice == "D":
print("drop")
else:
print("invalid")
答案 2 :(得分:0)
如果输入不是E/e
,则要继续执行,因此应该这样写。
choice = input("Enter A to add course, D to drop course, and E to exit: ")
choice = choice.upper()
courses = []
while choice != "E":
if choice == "A":
course1 = input("Enter a course to add: ")
courses.append(course1)
courses.sort()
print("Courses Registered: ", courses)
elif choice == "D":
drop1 = input("Enter a course to drop: ")
if drop1 in courses:
courses.remove(drop1)
print("Course Dropped")
courses.sort()
print("Courses Registered: ", courses)
else:
print("You are not registered in that class")
else:
print("Invalid input")
choice = input("Enter A to add course, D to drop course, and E to exit: ")
choice = choice.upper()
else:
print("You have exited the program")