【发布时间】:2022-01-18 17:43:54
【问题描述】:
我有这个 python 代码
color = input("Color? ")
color_list = ["orange", "red", "green", "blue", "pink"]
if color in color_list:
if color == "orange":
print("This is orange")
elif color == "red":
print("This is red")
else:
print("Color invalid")
有没有办法在同一个print() 上打印列表的不同元素,这样我就不必为列表中的每种颜色都写一个elif:
对不起,如果你听不懂我的话,我还在学习英语。
【问题讨论】:
-
你为什么不直接打印
color? -
你已经检查了它是否在列表中,为什么还要检查它是列表中的哪个具体的东西?
-
这里不需要使用if语句使用简单的
f string:print(f'This color is {color}')