首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何让python中的循环返回到原始的while语句。

如何让python中的循环返回到原始的while语句。
EN

Stack Overflow用户
提问于 2018-06-08 03:40:14
回答 1查看 44关注 0票数 0

我创建的循环在询问要添加哪个类以及何时删除类时运行得很流畅。但是,每当我尝试在删除一个类之后添加一个类时,程序就会结束,而不是返回到循环来添加一个类。我在程序中哪里出错了。下面是代码。

代码语言:javascript
复制
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'
EN

回答 1

Stack Overflow用户

发布于 2018-06-08 03:46:08

实际上...Registration作为一个变量在第一个input语句之后不会改变。这意味着当你开始运行这段代码时,无论你给它什么值,你都会被卡住。

由于您似乎想要类似菜单的功能,因此实现此功能的更简单方法是将所有内容拆分到方法中。

代码语言:javascript
复制
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循环。

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50749043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档