无法检查键是否属于值
下面的 short()
函数不起作用。 full()
工作得很好。有没有解决方案,或者我是否必须创建另一个交换键和值的目录?
import random
elements = {"Sc":"Scandium",
"Ti":"Titanium",
"V":"Vanadium",
"Cr":"Chromium",
"Mn":"Manganum",
"Fe":"Ferrum"}
def short():
question = random.choice(list(elements.values()))
print(question)
answer = input("What is the short name of this element?: ")
if answer == elements[question]:
print("Right")
def full():
question = random.choice(list(elements.keys()))
print(question)
answer = input("What is the full name of this element?: ")
if answer == elements[question]:
print("Right")
mode = input("Do you want to guess the short or full name? (sh/fu): ").lower()
if mode == "sh":
short()
elif mode == "fu":
full()
The short()
function below doesn't work. full()
works completely fine. Is there a solution, or do I have to make another directory where key and value are swapped?
import random
elements = {"Sc":"Scandium",
"Ti":"Titanium",
"V":"Vanadium",
"Cr":"Chromium",
"Mn":"Manganum",
"Fe":"Ferrum"}
def short():
question = random.choice(list(elements.values()))
print(question)
answer = input("What is the short name of this element?: ")
if answer == elements[question]:
print("Right")
def full():
question = random.choice(list(elements.keys()))
print(question)
answer = input("What is the full name of this element?: ")
if answer == elements[question]:
print("Right")
mode = input("Do you want to guess the short or full name? (sh/fu): ").lower()
if mode == "sh":
short()
elif mode == "fu":
full()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我改变了主意。您不需要反向
dict
。只是结构
short()
略有不同:short()
现在像full()
一样随机选择一个键,但用术语提出问题很容易在向前访问的长名称。I changed my mind. You don't need a reverse
dict
.Just structure
short()
slightly differently:short()
now selects a key at random just likefull()
, but asks the question in terms of the long name which is easily accessible in the forwards direction.