压缩 elif 语句
Compress elif statement
我正在学习 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")
你可以用字典,然后按键搜索。这将比在列表中搜索更快,尤其是当元素数量变大时(这可能需要检查列表中的每个元素,而不是只执行一次查找)。
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")
我正在学习 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")
你可以用字典,然后按键搜索。这将比在列表中搜索更快,尤其是当元素数量变大时(这可能需要检查列表中的每个元素,而不是只执行一次查找)。
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")