所以我已经把这个代码弄乱了近两天,由于一个空格错误,我无法让zybook接受它。这是代码:
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和空格并将其取出,但是即使输出看起来相同,我仍然会因为空白关闭而停靠点。我不知道如何解决它。