Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中添加检查列表并从列表中删除_Python_List_Python 2.7 - Fatal编程技术网

在Python中添加检查列表并从列表中删除

在Python中添加检查列表并从列表中删除,python,list,python-2.7,Python,List,Python 2.7,作业说明是这样的 打印一组简单的说明,供用户选择 开门的钥匙 因此,我认为实现这一目标的目标是 制作清单[彩虹钥匙] 打印清单(您有这些密钥) 请猜猜能开门的钥匙 它将检查库存,如果是红色键,它将打印“打开” 否则将打印“继续猜测”,并从库存中删除密钥 这就是我目前所拥有的。我还不知道如何添加和检查库存 keepGuess = True correctKey = "red" while keepGuess: guess = raw_input("Gues

作业说明是这样的

打印一组简单的说明,供用户选择 开门的钥匙

因此,我认为实现这一目标的目标是

制作清单[彩虹钥匙]

打印清单(您有这些密钥)

请猜猜能开门的钥匙

它将检查库存,如果是红色键,它将打印“打开”

否则将打印“继续猜测”,并从库存中删除密钥

这就是我目前所拥有的。我还不知道如何添加和检查库存

keepGuess = True
correctKey = "red" 

while keepGuess:
    guess = raw_input("Guess the key to open the door: ")

    if guess == correctKey:
            print ("You may enter")
            keepGuess = False
    else:
        
        print ("Keep guessing")
谢谢你帮助我。 以下是最终结果

keepGuess = True
correctKey = "blue"
keys = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]

print keys
print

while keepGuess:
    guess = raw_input("Which key will open the door? ")

    if guess == correctKey:
            print ("You may enter")
            keepGuess = False
    else:
        if guess in keys != "blue":
            keys.remove(guess)
            if guess  not in keys:
                print
                print ("The key didn't open the door.")
                print ("Keep guessing")
                print
                print keys
                print
它打印了这个

['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']

Which key will open the door? red

The key didn't open the door.
Keep guessing

['orange', 'yellow', 'green', 'blue', 'indigo', 'violet']

Which key will open the door? red
Which key will open the door? blue
You may enter

如果没有关于错误检查的更多信息,我无法确定我是否解决了问题。但是,我认为您需要维护一个简单的键列表,例如:

door_key_inv = ["red", "yellow", "paisley-print chartreuse"]
您以[]的形式启动列表(即为空),并在找到键时添加键

现在,当用户输入猜测时,您必须进行两项检查:

  • 这是库存中的关键颜色吗?如果是,则转至步骤2;如果没有,请打印警告

    如果在门中猜测\u键\u inv:

  • 这是正确的钥匙吗?如果是这样,打开门并打破循环

  • 循环返回以获得下一个猜测

  • 这就是你需要的吗?

    你非常接近了。您可以初始化一个空列表来存储库存。当有人猜到一个键时,您只需将它附加到列表中即可。当然,我们将检查猜测的密钥是否已经在库存中,如果已经在库存中,我们将不添加它

    keepGuess = True
    correctKey = "red" 
    inventory = []
    
    while keepGuess:
        guess = raw_input("Guess the key to open the door: ")
    
        if guess == correctKey:
                print ("You may enter")
                inventory.append(guess)
                keepGuess = False
        else:
            if guess not in inventory:
                inventory.append(guess)
            else:
                print ("You have already added this key to your inventory.")
            print ("Keep guessing")
    
    这里有一个测试:

    Guess the key to open the door: blue
    Keep guessing
    Guess the key to open the door: blue
    You have already added this key to your inventory.
    Keep guessing
    Guess the key to open the door: red
    You may enter
    

    你为什么需要库存?如果他们没有红钥匙怎么办?如果他们选择了一把他们没有的钥匙,会发生什么?你说的添加和检查库存是什么意思?我认为这就完成了这项任务所需的结果。我确实想添加一些显示库存和检查大写颜色的内容,但你不必帮我。谢谢你的帮助@没有问题。如果您遇到任何障碍,请随时回复此评论,我会回复:)