压缩 elif 语句

发布于 2025-01-10 00:32:02 字数 1306 浏览 2 评论 0原文

我正在学习 Python,想知道这是否是压缩下面代码中的 elif 语句的方法。我想要一个可以循环此数据结构的函数,而不是 12 个不同的 elif 语句。有人可以帮助我吗?

character = input("Write a character from A-Z: ")
list = [["A",4],["A-",3.7],["B+",3.3],["B",3],["B-",2.7],["C+",2.3],["C",2],
        ["C-",1.7],["D+",1.3],["D",1],["D-",0.7],["F",0]]
text = "The value number is "

if character in list[0]:
    print(text + str(float(list[0][1])))
elif character in list[1]:
    print(text + str(float(list[1][1])))
elif character in list[2]:
    print(text + str(float(list[2][1])))
elif character in list[3]:
    print(text + str(float(list[3][1])))
elif character in list[4]:
    print(text + str(float(list[4][1])))
elif character in list[5]:
    print(text + str(float(list[5][1])))
elif character in list[6]:
    print(text + str(float(list[6][1])))
elif character in list[7]:
    print(text + str(float(list[7][1])))
elif character in list[8]:
    print(text + str(float(list[8][1])))
elif character in list[9]:
    print(text + str(float(list[9][1])))
elif character in list[10]:
    print(text + str(float(list[10][1])))
elif character in list[11]:
    print(text + str(float(list[11][1])))
elif character in list[12]:
    print(text + str(float(list[12][1])))
else:
    print("Write a character from A-F")

I'm learning Python and would like to know if it's a way to compress the elif statements in the code below. Instead of having 12 different elif statements, I would like a function that can loop over this data structure. Can someone could help me?

character = input("Write a character from A-Z: ")
list = [["A",4],["A-",3.7],["B+",3.3],["B",3],["B-",2.7],["C+",2.3],["C",2],
        ["C-",1.7],["D+",1.3],["D",1],["D-",0.7],["F",0]]
text = "The value number is "

if character in list[0]:
    print(text + str(float(list[0][1])))
elif character in list[1]:
    print(text + str(float(list[1][1])))
elif character in list[2]:
    print(text + str(float(list[2][1])))
elif character in list[3]:
    print(text + str(float(list[3][1])))
elif character in list[4]:
    print(text + str(float(list[4][1])))
elif character in list[5]:
    print(text + str(float(list[5][1])))
elif character in list[6]:
    print(text + str(float(list[6][1])))
elif character in list[7]:
    print(text + str(float(list[7][1])))
elif character in list[8]:
    print(text + str(float(list[8][1])))
elif character in list[9]:
    print(text + str(float(list[9][1])))
elif character in list[10]:
    print(text + str(float(list[10][1])))
elif character in list[11]:
    print(text + str(float(list[11][1])))
elif character in list[12]:
    print(text + str(float(list[12][1])))
else:
    print("Write a character from A-F")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

饭团 2025-01-17 00:32:02

您可以使用字典,然后按键进行搜索。这比在列表中搜索更快,尤其是当元素数量变大时(这可能需要检查列表中的每个元素,而不是仅仅执行单个查找)。

character = input("Write a character from A-Z: ")
data = {"A": 4, "A-": 3.7, "B+": 3.3, "B": 3, "B-": 2.7,
    "C+": 2.3, "C": 2, "C-": 1.7, "D+": 1.3, "D": 1, "D-": 0.7, "F": 0}
        
if character in data:
    print(f"The value number is {data[character]}")
else:
    print("Write a character from A-F")

You can use a dictionary, and then search by key. This will be faster than searching in a list, especially when the number of elements gets large (which requires possibly examining every element in the list, rather than just performing a single lookup).

character = input("Write a character from A-Z: ")
data = {"A": 4, "A-": 3.7, "B+": 3.3, "B": 3, "B-": 2.7,
    "C+": 2.3, "C": 2, "C-": 1.7, "D+": 1.3, "D": 1, "D-": 0.7, "F": 0}
        
if character in data:
    print(f"The value number is {data[character]}")
else:
    print("Write a character from A-F")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文