python - 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 和空格并将其取出,但即使输出看起来相同,我仍然会因为空格关闭而得到停靠点。我不知道如何解决它。
解决方案
推荐阅读
- algorithm - 随机快速排序分区概率
- python - 将 .jpg 转换为 .txt
- google-cloud-platform - 使用 Cloud 设计高效的 JSON 文档全文搜索的建议。大表
- node.js - 节点更新后sinon js断言错误
- python - 如何检查列表中的数字是否大于它之前的数字 - python
- wolfram-mathematica - 使用给定的数字参数标记文件的名称
- php - PHP:我需要在 sql 查询中没有可用数据的月份填写“0”
- javascript - 未捕获的 ReferenceError:$ 未在 DataTable 上定义
- react-native - 由于一些内存不足错误,反应本机应用程序崩溃如何解决这个问题
- javascript - 单击浏览器后退按钮时重新加载当前页面(在所有浏览器中)