退出For Loop&枚举(Python)

时间:2018-05-31 12:21:44

标签: python loops for-loop exit enumerate

我是Python的新手,正在开发一个bootcamp项目...而且我绝对比它需要的更难......

我最终要做的是创建以下内容:

  1. 姓名:泰坦尼克号\ n 导演:斯皮尔伯格\ n 年份:1997年\ n \ n

  2. 名称:The Matrix \ n 主任:Waskowskis \ n 年份:1996年\ n \ n

  3. AFTER 我添加了“(A)dd电影功能......所以,首先,我似乎无法'退出'For循环...一旦我跑它,它只是无限期地重复......除此之外,如果我尝试使用“枚举”,我无法正确格式化。

    这是我的代码:(我正在谈论的部分是在“def show_movies”功能下:

    import sys
    import random
    import os
    movies = []
    
    
    def menu():
        global user_input
        print("Welcome to 'The Movie Program!!'")
        print("(A)dd movie to your list")
        print("(L)ist movies you've added")
        print("(S)earch for movies in your list")
        user_input = str(input("Which function would you like to do?:\n\n""Selection: ").capitalize())
        while user_input != 'Q':
            if user_input == 'A':
                add_movies()
            elif user_input == 'L':
                show_movies()
            elif user_input == 'A':
                search_movies()
            else:
                print("\n\n--Unknown command--Please try again.\n")
                print("Welcome to 'The Movie Program!!'")
                print("(A)dd movie to your list")
                print("(L)ist movies you've added")
                print("(S)earch for movies in your list")
                user_input = str(input("Which FUNCTION would you like to do?:\n\n""Selection: ").capitalize())
    
    
    def add_movies():
        #name = (input('What is the title of the movie?: ').title())
        #director = str(input("Who was the director of this movie?: ").title())
        year = None
        while True:
            try:
                name = (input('What is the title of the movie?: ').title())
                director = str(input("Who was the director of this movie?: ").title())
                year = int(input("What was the release year?: "))
            except ValueError:
                print("Only numbers, please.")
                continue
            movies.append({
                "name": name,
                "director": director,
                "year": year
            })
            break
        menu()
    
    
        add_movies()
    
    
    def show_movies():
            for movie in movies:
                print(f"Name: {movie['name']}")
                print(f"Director: {movie['director']}")
                print(f"Release Year: {movie['year']}\n")
                #continue
                #break
    
    
    def search_movies():
        movies
        print("This is where you'd see a list of movies in your database")
    
    
    menu()
    

1 个答案:

答案 0 :(得分:1)

问题出在你的while user_input != 'Q':循环中。

如果user_input等于L,则会调用show_movies(),但不会要求更多输入。它只围绕while循环,每次调用show_movies()

每次循环时都应再次输入user_input,而不仅仅是else子句。

while user_input != 'Q':
    if user_input == 'A':
        add_movies()
    elif user_input == 'L':
        show_movies()
    elif user_input == 'A':
        search_movies()
    else:
        print("\n\n--Unknown command--Please try again.\n")
        print("Welcome to 'The Movie Program!!'")
        print("(A)dd movie to your list")
        print("(L)ist movies you've added")
        print("(S)earch for movies in your list")
    # the next line is now outside your `else` clause
    user_input = str(input("Which FUNCTION would you like to do?:\n\nSelection: ").capitalize())