如何转到并完成一个子功能,然后返回到我所使用的“菜单功能”((Python)

2024-09-10 15:13:50 发布

您现在位置:Python中文网/ 问答频道 /正文

理想情况下,我希望在“添加电影”过程中有一个步骤来确认它是一年的整数,一旦被接受,在成功添加电影后继续返回主菜单

(我不想问,也不想问我希望它如何将用户输入数据保存到一个文件中,以便从中提取数据,这样我就不会每次都做一个新条目,哈哈)

这是我得到的。。。再一次,这是我在27小时课程中大概4小时后的努力

我希望你们能理解我想要实现的目标。。。但它只是在“添加电影”功能之后没有正确地“完成”。。。你应该明白我在说什么

import sys
import random
import os

movies = []

def menu():
    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")
            menu()

            #user_input = str(input("\nWhich function would you like to do?:\n"
            #                   "(A)dd movie to your list\n"
            #                   "(L)ist movies you've added\n"
            #                   "(S)earch for movies in your list\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())
    while True:
        try:
            year = int(input("What was the release year?: "))
            return year
            break
        except ValueError:
            print("Only numbers, please.")
        year = False

        #return year

        #if year < 0:
        #    print("Sorry,your response must not be negative.")
        #    continue
        #else:
        #    return year

    movies.append({
        "name": name,
        "director": director,
        "year": year
    })


def show_movies():
    movies[::3]
    for c, value in enumerate(movies, 1):
        print(c, value)
    for movie in movies:
        print(f"Name: {movie['name']}")
        print(f"Director: {movie['director']}")
        print(f"Release Year: {movie['year']}\n")
'''Not done yet
def search_movies():
    movies
    print("This is where you'd see a list of movies in your database")

'''
menu()

Tags: tonameinyouforinputyourdef
1条回答
网友
1楼 · 发布于 2024-09-10 15:13:50

user_input = str(input("Which function would you like to do?:\n\n""Selection: ").capitalize())

在while循环内部,并在循环之前为用户_输入值

就像这样:

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

此外,还应将电影制作为函数的变量和返回值,或将其设置为全局变量

相关问题 更多 >