【问题标题】:I cannot access the movies list I created (python)我无法访问我创建的电影列表(python)
【发布时间】: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]

标签: python list indexing menu


【解决方案1】:

在 m[1] 处提取电影名称而不是年份。

将年份输入转换为整数并进行比较。

if int(year) == m[0]:

此外,Breaks 使用不当。显示选项索引未正确使用。

 print(m[0], m[1], m[2])

更正的代码


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 = input("Please enter a valid year: ")
        print(year)
        for m in movies:
            if int(year) == m[0]:
                print("The winning movie in ", year," was: ", m[1])
    elif option == "2":
        year = input("Please enter a valid year: ")
        for m in movies:
            if int(year) == m[0]:
                print("The winning movie was: ", m[1], "and its category was: ", m[2])

    elif option == "d":
        for m in movies:
            print(m[0], m[1], m[2])

    elif option == "dc":
        cat = input("Please enter a valid category : ")
        for m in movies:
            if cat == m[2]:
                print(m[1], m[2])
    else:

        print("Please select a valid option")

【讨论】:

  • ohhhh 好吧,我明白了,所以我的格式和不必要的中断必须在第一个条目时停止阅读列表。非常感谢!!
猜你喜欢
  • 1970-01-01
  • 2016-02-29
  • 2015-02-19
  • 2021-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-06
  • 2014-01-15
相关资源
最近更新 更多