无法检查键是否属于值

发布于 2025-01-11 23:36:04 字数 865 浏览 0 评论 0原文

下面的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

财迷小姐 2025-01-18 23:36:04

我改变了主意。您不需要反向dict

只是结构 short() 略有不同:

import random

elements = {"Sc":"Scandium",
           "Ti":"Titanium",
           "V":"Vanadium",
           "Cr":"Chromium",
           "Mn":"Manganum",
           "Fe":"Ferrum"}

def short():
    question = random.choice(list(elements.keys()))
    print(elements[question])
    answer = input("What is the short name of this element?: ")
    if answer == 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()

short() 现在像 full() 一样随机选择一个键,但用术语提出问题很容易在向前访问的长名称。

I changed my mind. You don't need a reverse dict.

Just structure short() slightly differently:

import random

elements = {"Sc":"Scandium",
           "Ti":"Titanium",
           "V":"Vanadium",
           "Cr":"Chromium",
           "Mn":"Manganum",
           "Fe":"Ferrum"}

def short():
    question = random.choice(list(elements.keys()))
    print(elements[question])
    answer = input("What is the short name of this element?: ")
    if answer == 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()

short() now selects a key at random just like full(), but asks the question in terms of the long name which is easily accessible in the forwards direction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文