在Python2.7中,检查原始输入是否不在字符串列表中的方法是什么?

2024-09-27 17:31:49 发布

您现在位置:Python中文网/ 问答频道 /正文

为了节省空间,我事先切断了177行,因为我有一个列表,其中包含了所有可能的方式来键入不同的盾牌我有,然后盾牌被打破,以进一步检查什么是给定的,并给予适当的统计提升。如果输入不在列表中,我希望人们必须重新输入一个选择,直到他们键入列表中的某个内容。你知道吗

print "these are the possible shields you can use:" ', '.join(defendList)
Choose_A_Shield = raw_input('choose a valid shield from the list please:')
if Choose_A_Shield in sheild:
    defending_defense = base_defense + 6
elif Choose_A_Shield in large_Shield:
    defending_defense = base_defense + 9
elif Choose_A_Shield in nothing:
    print "you decide to go without a shield"
    defending_defense = 10
Choose_A_Shield = raw_input('choose a shield from the list to use:')
Choose_A_Shield = raw_input('choose a shield from the list to use:')
while Choose_A_Shield != item in defending_list:
    Choose_A_Shield = raw_input('choose a valid shield from the list please:')
    if Choose_A_Shield in sheild:
        defending_defense = base_defense + 6
    elif Choose_A_Shield in large_Shield:
        defending_defense = base_defense + 9
    elif Choose_A_Shield in nothing:
        print "you decide to go without a shield"
        defending_defense = 10

我试图为python中基于文本的游戏中的设备设置创建这样的语句,但是,环顾四周,我找不到一种简单的方法来检查它是否不在列表中,然后在循环中继续,直到输入在列表中。你知道吗

这是我当前的错误消息:

Traceback (most recent call last):
  File "python", line 1, in <module>
  File "python", line 170, in story
NameError: global name 'item' is not defined

你能告诉我如何完成这项任务吗?你知道吗

另外,如果你愿意,你能告诉我为什么代码:

    print "these are the possible shields you can use:" ', '.join(defendList)

给我一些这样的文字:

nothingthese are the possible shields you can use:, shield

字符串所连接的列表从一开始就包含“nothing”(例如,如果一个人想赤手空拳作战,则为其添加一个项目)如果你拿起一个类似盾牌或大盾牌的项目,它会将盾牌或大盾牌添加到该项目中

这对我来说都是个大问题,因为我计划使用同一个系统让玩家选择武器。你知道吗


Tags: theinyou列表inputrawuselist
3条回答

你的第一个问题应该很简单:

while Choose_A_Shield not in defending_list:

为了正确连接,添加+

print "these are the possible shields you can use: " + ', '.join(defendList)

如果您的设备列表是list_of_shields,您可以像这样实现您想要的:

choice = raw_input('choose a valid shield from the list please:')
while choice not in list_of_shields:
    choice = raw_input('choose a valid shield from the list please:')

很简单: 要检查原始输入是否在这样的字符串列表中,只需使用“inpt not in blah\u list”。 要添加到字符串而不让它们变得奇怪,必须关闭引号并使用“+”。你知道吗

这个答案够好吗?我没什么可说的了。。。等待。。。这是必需的吗?你知道吗

相关问题 更多 >

    热门问题