我是Python的新手,正在做一个训练营项目...而且我绝对让它变得比它需要的更难......
我最终要做的是创建以下内容:
-
名称: 泰坦尼克号 导演: 斯皮尔伯格 年份: 1997 \
-
名称:矩阵 导演: 瓦斯科夫斯基斯 年份: 1996 \
在我用"(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?:nn""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("nn--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?:nn""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()
问题出在您的while user_input != 'Q':
循环中。
如果user_input
等于L
,则它调用show_movies()
,但不要求更多输入。它只是在每次调用show_movies()
的while
循环中转一圈又一圈。
每次通过循环都应再次输入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("nn--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?:nnSelection: ").capitalize())