简体   繁体   中英

Exiting a For Loop & Enumeration (Python)

I'm new to Python and working on a bootcamp project... and I'm absolutely making it harder than it needs to be...

What I'm looking to ultimately do, is create the following:

  1. Name: Titanic \\n Director: Spielberg \\n Year: 1997 \\n\\n

  2. Name: The Matrix \\n Director: Waskowskis \\n Year: 1996 \\n\\n

AFTER I've added them with the "(A)dd Movie function... So, firstly, I can't seem to 'exit' the For Loop... once I run it, it just repeats indefinitely... and beyond that, I'm not able to get the formatting correct if I try to use "enumerate".

Here's my code: (the portion I'm talking about is under the "def show_movies" function:

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()

The problem is in your while user_input != 'Q': loop.

If user_input is equal to L , then it calls show_movies() , but doesn't ask for more input. It just goes round and round the while loop calling show_movies() each time.

You should input user_input again each time through the loop, not only in your else clause.

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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM