繁体   English   中英

如何在 Python 中使用带有 If/Else 的列表

[英]How Can I Use Lists With If/Else In Python

list= ["hi","hello","how are u"]
question= input("please write something")

if list in question:
 print("1")

if question in list:
 print("2")

嘿,我正在尝试做一些事情,我想使用带有 if else 语句的列表。 当用户写一些例子“你好,你在做什么”时,当 if 语句看到“你好”程序会说些什么。

if list in question:

上一行将整个列表与问题进行比较,因此: ["hi", "hello", "how are u"] 是否等于问题? 而且您也无法将列表 object 与字符串 object 进行比较

list = ["hi", "hello", "how are u"]
question = input("please write something: ")
for each in list:
    if each in question:
        print("Hi!")
        break

这应该工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM