【发布时间】:2017-12-04 18:04:54
【问题描述】:
我想用 Python 3 制作一个基本上是词汇卡片的程序。我将能够列出术语、添加术语或显示随机定义以尝试准确猜测。一旦准确猜测,我将可以选择另一个定义来猜测。或者,我希望能够显示一个随机键:值对,并继续查看对,直到我输入 EXIT。
我已经使用字典制作了大部分程序,但不知道如何输入正确的命令来输入显示定义的键。如果有人可以提供建议,我将不胜感激!此外,我在输入此代码时收到了一些错误消息,并且不得不做一堆缩进,不知道我在那里做错了什么。
import random
terms = {"1" : "def 1", #Dictionary of 'terms' and 'definitions'
"2" : "def 2",
"3" : "def 3"}
menu = None
while menu != "4":
print("""
DIGITAL FLASHCARDS!
1 - List Terms
2 - Add Term
3 - Guess Random Definition
4 - Exit
""")
menu = input("\t\t\tEnter Menu option: ")
if menu == "1": # List Terms
print("\n")
for term in terms:
print("\t\t\t", term)
input("\n\tPress 'Enter' to return to Main Menu.\n")
elif menu == "2": # Add Term
term = input("\n\tEnter the new term: ").upper()
if term not in terms:
definition = input("\tWhat is the definition? ")
terms[term] = definition
print("\n\t" + term, "has been added.")
else:
print("\n\tThat term already exists!")
input("\n\tPress 'Enter' to return to Main Menu.\n")
elif menu == "3": # Guess Random Definition. Once correct, choose new random definition
print("\n\t\t\tType 'Exit' to return to Menu\n")
choice = random.choice(list(terms.values()))
print("\n\t" + choice + "\n")
guess = None
while guess != "EXIT":
guess = str(input("\tWhat is the term? ")).upper()
【问题讨论】:
-
你的缩进不一致,最明显的是在add term部分
-
我知道您列出了您的问题,但我仍然无法弄清楚您需要什么。你能帮我改一下吗?
-
嗨,科里。要重现您在文本编辑器中的间距(这对于调试 Python 至关重要),最简单的方法是复制并粘贴到问题编辑框中,然后突出显示粘贴的文本并按 ctrl-k。这会将每行向前移动 4 个空格,这正是您想要的。
-
@juanpa.arrivillaga:如果代码使用制表符和空格的混合缩进(这里就是这种情况),那将不起作用。
-
@martineau 啊啊好点。 Cory:不要在 Python 中混用制表符和空格。大多数人只是坚持使用 4 个空格。
标签: python python-3.x dictionary