首页 > 解决方案 > Python School Project 问题,空白问题

问题描述

所以我已经搞砸了这段代码将近两天了,由于一个空格错误,我无法让 zybooks 接受它。这是代码:

def main():
movie_collection = {"Munich":[2005, "Steven Spielberg"],
                       "The Prestige": [2006, "Christopher Nolan"],
                       "The Departed": [2006, "Martin Scorsese"],
                       "Into the Wild": [2007, "Sean Penn"],
                       "The Dark Knight": [2008, "Christopher Nolan"],
                       "Mary and Max": [2009, "Adam Elliot"],
                       "The King\'s Speech": [2010, "Tom Hooper"],
                       "The Artist": [2011, "Michel Hazanavicius"],
                       "The Help": [2011, "Tate Taylor"],
                       "Argo": [2012, "Ben Affleck"],
                       "12 Years a Slave": [2013, "Steve McQueen"],
                       "Birdman": [2014, "Alejandro G. Inarritu"],
                       "Spotlight": [2015, "Tom McCarthy"],
                       "The BFG": [2016, "Steven Spielberg"]}
prompt_year = True
while prompt_year:
    user_year = int(input("Enter a year between 2005 and 2016:\n"))
    if user_year<2005 or user_year>2016:
        print("N/A")
    else:
        for key, value in movie_collection.items():
            if value[0] == user_year:
                print(key + ", " + str(value[1]) )
        prompt_year = False
menu = "MENU" \
       "\nSort by:" \
       "\ny - Year" \
       "\nd - Director" \
       "\nt - Movie title" \
       "\nq - Quit\n"
prompt_user = True
while prompt_user:
    prev_val = ''
    print("\n" + menu)
    user_choice = input("Choose an option:")
    if user_choice == "q":
        prompt_user = False
    elif user_choice == "y":
        for key, value in sorted(movie_collection.items(), key=lambda item: (item[1], item[0])):
            if(prev_val!=value[0]):
                print ("\n",value[0],end=':\n',sep='')
                prev_val = value[0]
            print("\t"+key + ", " + str(value[1]))
    elif user_choice == "d":
        for key, value in sorted(movie_collection.items(), key=lambda key_value: key_value[1][1]):
            if(prev_val!=value[1]):
                print("\n",value[1],end=':\n',sep='')
                prev_val = value[1]
            print("\t" + key + ", " + str(value[0]))
    elif user_choice == "t":
        for key, value in sorted(movie_collection.items(), key=lambda item: (item[0], item[1])):
            if(prev_val!=key):
                print("\n",key,end=':\n',sep='')
                prev_val = key
            print("\t" + str(value[1]) + ", " + str(value[0]))
    else:
        print("Invalid input")
main()

出于某种原因,程序在“选择一个选项:”输入后给了我一个空格错误。我尝试添加 \n 和空格并将其取出,但即使输出看起来相同,我仍然会因为空格关闭而得到停靠点。我不知道如何解决它。

标签: pythonwhitespace

解决方案


推荐阅读