我创建的循环在询问要添加哪个类以及何时删除类时运行得很流畅。但是,每当我尝试在删除一个类之后添加一个类时,程序就会结束,而不是返回到循环来添加一个类。我在程序中哪里出错了。下面是代码。
RegisteredCourses=[]
Registration=raw_input('Enter A to add a course, D to drop a course and E to exit.')
while Registration=='a':
Course=raw_input('What course do you want to add?')
RegisteredCourses.append(Course)
print RegisteredCourses
Registration=raw_input('Enter A to add a course, D to drop a course and E to exit.')
while Registration=='d':
DropCourse=raw_input('What course do you want to drop?')
RegisteredCourses.remove(DropCourse)
print RegisteredCourses
Registration=raw_input('Enter A to add a course, D to drop a course and E to exit.')
while Registration=='e':
print 'bye'
发布于 2018-06-08 03:46:08
实际上...Registration
作为一个变量在第一个input语句之后不会改变。这意味着当你开始运行这段代码时,无论你给它什么值,你都会被卡住。
由于您似乎想要类似菜单的功能,因此实现此功能的更简单方法是将所有内容拆分到方法中。
def add_course():
Course=raw_input('What course do you want to add?')
RegisteredCourses.append(Course)
print RegisteredCourses
# Other methods for other functions
在应用程序的核心内部,您有一个简单的while True
循环。
while True:
registration = raw_input('Enter A to add a course, D to drop a course and E to exit.')
if registration == 'a':
add_course()
# Other methods
if registration == 'e':
print 'bye'
break
https://stackoverflow.com/questions/50749043
复制相似问题