简体   繁体   English

如何使用字典/列表创建if语句

[英]How would I make an if statement with a dictionary/list

My code: 我的代码:

hello = ['hi',]  
i=input("> ")
if i==hello:
    print("hi")
else:
    print("no")

I wanted to do this for making a text-based game, so the user can enter almost anything similar to what is needed to move on; 我想这样做是为了制作一个基于文本的游戏,所以用户几乎可以输入任何类似于继续前进的东西; it does not have to be a list or a dictionary, just something that would work. 它不一定是列表或字典,只是一些可行的东西。

Perhaps you would like to use in : 也许你想使用in

hello = ["hi there", "hello", "hey"]
i=input("> ")
if i in hello:
    print("hi")
else:
    print("no")
hello = set(['hi', 'hello'])
if i in hello:
    # ...

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

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