gpt4 book ai didi

python - 创建一个 "Flashcard"词汇程序

转载 作者:行者123 更新时间:2023-11-28 21:41:22 26 4
gpt4 key购买 nike

我想用 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()

最佳答案

display a random definition to try and accurately guess. Once guessed accurately, I would be given the option for another definition to guess

使用terms.items()同时获取key和value。

将生成新定义问题的过程定义到函数 generate_question() 中以避免口是心非。

elif menu == "3": # Guess Random Definition. Once correct, choose new random definition
print("\n\t\t\tType 'Exit' to return to Menu\n")
def generate_question():
term, definition = random.choice(list(terms.items()))
print("\n\t" + definition + "\n")
return term
term = generate_question()
guess = None
while guess != "EXIT":
guess = input("\tWhat is the term? ").upper()
if guess == term:
print("Correct!")
if input("\tAnother definition?(y/n)").upper() in ["Y", "YES"]:
term = generate_question()
else:
break

Alternatively, I'd like to just be able to display a random key:value pair, and to continue viewing pairs until I enter EXIT.

elif menu == "4": # Random display a term-definition pair.
print("\n\t\t\tType 'Exit' to return to Menu\n")
exit = None
while exit != "EXIT":
term, definition = random.choice(list(terms.items()))
print(term + ":", definition)
exit = input("").upper() # Press enter to continue.

记得修改开头部分:

while menu != "5":
print("""

DIGITAL FLASHCARDS!

1 - List Terms
2 - Add Term
3 - Guess Random Definition
4 - View term-definition pairs
5 - Exit

""")

关于python - 创建一个 "Flashcard"词汇程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44852213/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com