Skip to main content
 首页 » 编程设计

python之如何定义一组变量,以便程序响应列表中的任何项目被调用,python3

2025年05月04日20落叶

我创建了一个列表,因此如果有人输入列表中的任何项目,它将打印相同的内容。

lista = ["example", "example2"] 
listb = ["exampleb", "example2b"] 
choice = input() 
if choice == lista[]: 
    print("outputa") 
elif choice == listb[]: 
    print("outputb") 

如果用户键入 example 或 example2,它将打印 outputa,但如果用户键入 exampleb 或 example2b,它将打印 outputb。 谢谢

请您参考如下方法:

lista = ["example", "example2"] 
listb = ["exampleb", "example2b"] 
choice = input() 
if choice in lista: 
    print("outputa") 
elif choice in listb: 
    print("outputb")