如果我必须要求用户从描述的字典中选择一个选项,我将如何在 python 中使用 if 语句?


color = {"1":"Violet","2":"Indigo","3":"Blue","4":"Green","5":"Yellow","6":"Orange","7":"Red"}
print ("Select your choice")
print (color)
color1 = input ("Select the first color: ")
for color1 in color:
if color1 != color:
print ("Please choose the correct option!")
if color1 in color:
col = color[color1]
print(col)

打印颜色(例如:红色(,如果颜色的编号在列表/字典中

if color1 in color.values():
#ETC

打印颜色(例如:红色(,如果颜色的名称在列表/字典中

感谢凯尔·阿尔姆的澄清。 那里不需要for color1 in color: if color1 != color: print ("Please choose the correct option!")

相关内容

最新更新