【发布时间】:2021-03-08 23:43:12
【问题描述】:
我目前在用户输入菜单选项后让我的程序访问我的列表时遇到问题。唯一被读取的条目是 1939 条目,任何其他年份都只会显示“输入有效年份”选项。另一个问题是在显示类别时,只会弹出前 2 部戏剧电影,其他所有类别在被要求时都不起作用。当我调用显示它时,它也不会显示整个列表(只会弹出 1939 条目)。任何帮助将不胜感激!
movies = [[1939,'Gone With the Wind','drama'],
[1943,'Casablanca','drama'],
[1965,'The Sound of Music','musical'],
[1969,'Midnight Cowboy','drama'],
[1972,'The Godfather','drama'],
[1973,'The String','comedy'],
[1977,'Annie Hall','comedy'],
[1981,'Chariots of Fire','drama'],
[1984,'Amadeus','historical'],
[1986,'Platoon','action'],
[1988,'Rain Man','drama'],
[1990,'Dances with Wolves','western'],
[1992,'Unforgiven','western'],
[1993,'Schindlers List','historical'],
[1994,'Forrest Gump','comedy'],
[1995,'Braveheart','historical'],
[1997,'Titanic','historical'],
[1998,'Shakespeare in Love','comedy'],
[2001,'A Beautiful Mind','historical'],
[2002,'Chicago','musical'],
[2009,'The Hurt Locker','action'],
[2010,'The Kings Speech','historical'],
[2011,'The Artist','comedy'],
[2012,'Argo','historical'],
[2013,'12 Years a Slave','drama'],
[2014,'Birdman','comedy'],
[2016,'Moonlight','drama'],
[2017,'The Shape of Water','fantasy'],
[2018,'Parasite','comedy']]
menu = """
1 - Display winning movie for a selected year
2 - Display movie and category for a selected year
d - Display entire movie list
dc - Display movies in a selected category - year and title
q = quit
"""
while True:
print(menu)
option = input("Please enter a valid menu option from above: ")
if option in 'qQ':
print("Ending program")
break
elif option == "1":
year = int(input("Please enter a valid year: "))
for m in movies:
if year == m[1]:
print("The winning movie in ", year," was: ", m[1])
else:
print("Input a valid year")
break
elif option == "2":
year = int(input("Please enter a valid year: "))
for m in movies:
if year == m[0]:
print("The winning movie was: ", m[1], "and its category was: ", m[2])
else:
print("Please enter a valid year")
break
elif option == "d":
for m in movies:
print(m[0], m[1], m[2])
break
elif option == "dc":
cat = input("Please enter a valid category")
for m in movies:
if cat == m[2]:
print(m[0], m[1])
else:
print("Please enter a valid category")
break
else:
print()
print("Please select a valid option")
【问题讨论】:
-
列表索引从 0 开始,而不是 1。所以应该是
m[0]到m[2]