条件被忽略?

Condition being ignored?

所以,我对 Python 比较陌生,如果这个问题看起来很愚蠢,我提前道歉。

在练习游戏中,我试图让玩家了解如何制作我自己的基于文本的冒险,玩家被问及他们是否会继续并且他们必须使用他们的灯笼继续沿着隧道前进,随后说这个;

elif nolamp and lan2 == ("use lantern") or lan2 == ("USE LANTERN") or lan2 == ("Use lantern") or lan2 == ("use LANTERN") or lan2 == ("Use LANTERN"):
  litlamp = True
  nolamp = False
  tunnel2 = True
  print ("")
  print ("You light the lantern and can now see.")
  print ("The tunnel is still very dark ahead.")
  time.sleep(2)
  print ("The walls look wet.")
  time.sleep(1.6)
  print ("Will you go back?")
  print ("")

如果他们的灯笼没有亮着,它会打印出来;

elif nolamp and lan2 == ("n") or lan2 == ("N") or lan2 == ("no") or lan2 == ("NO") or lan2 == ("No"):
  print ("You would continue, but it's too dark and cold.")
  print ("To go any farther like this would be a bad idea.")
  time.sleep(2.5)
  print ("Will you go back?")

上面的语句在单独的 while 循环中定义;

  lanternv2 = True
  tunnelv1 = True
  nolamp = False
  tunnel2 = False
  litlamp = False

当他们的灯笼被点亮时,玩家应该继续;

elif tunnel2 and lan2 == ("n") or lan2 == ("N") or lan2 == ("no") or lan2 == ("NO") or lan2 == ("No"):
  tunnel3 = True
  tunnel2 = False
  print ("You continue onward.")
  print ("You still see no light and the entrance is disappearing behind you.")
  time.sleep(2.5)
  print ("It's very very cold.")
  time.sleep(1.1)
  print ("Will you go back?")

但是我的问题是,当玩家点亮灯笼时尝试使用除“n”以外的任何其他答案时,将打印的内容就好像他们的 lamp 没有被使用一样。

有谁知道为什么会这样,我需要更具体一点吗?再次感谢,抱歉,如果这是一个愚蠢的问题。

我无法从代码中了解您想要的内容,但希望下面的内容能为您提供一些可以遵循的想法。具体来说,你会问“当他们的灯笼点亮时,除了“n”之外的任何其他答案[但它打印]就好像他们的 lamp 没有被使用过一样”。唯一不是“否”的答案是“使用灯笼选项”,但该选项会检查玩家是否没有 lamp。而其他陈述只会在您回答“否”时打印。所以在我看来,正在打印的代码部分不包括在问题中。

我尽量让它简单易懂,也许它可以提供一些线索!反正我写得很开心。

import random
# I tried to keep the code similar to yours
lanternv2 = True
tunnelv1 = True
nolamp = False
tunnel2 = False
litlamp = False
lan2 = ""
  
#The statements are defined above within a separate while loop;
while lan2 != "no" and lan2 != "n" and lan2 != "y":
    lan2 = input("Do you want to use a lantern to enter the dark and very spooky cave?")
    lan2 = lan2.lower()
 

if lan2 == "y":
    # Player has entered the cave
    litlamp = True
    nolamp = False
    tunnel2 = True
    inCave = True #always true while in the cave

    print ("")
    print ("You light the lantern and can now see.")
    print ("The tunnel is still very dark ahead.")
    #time.sleep(2)
    print ("The walls look wet.")
    #time.sleep(1.6)
      
    while(inCave):
        # generates a random number 0 - 1
        if(random.random() <.1):
            print("Your lantern sputters and the light disappears")
            litlamp = False
            
        caveInput = input("Will you go back?").lower()

        if not litlamp and (caveInput == ("n") or caveInput == ("no")):
              print ("You would continue, but it's too dark and cold.")
              print ("To go any farther like this would be a bad idea.")
              #time.sleep(2.5)
              print ()

          #The player should continue when their lantern is lit with this;
        elif litlamp and (caveInput == ("n") or caveInput == ("no")):
              # They can only continue into the cave
              print ("You continue onward.")
              print ("You still see no light and the entrance is disappearing behind you.")
              #time.sleep(2.5)
              print ("It's very very cold.")
              #time.sleep(1.1)
          
        else: # should check if yes but this is taking too long lol
              print("you flee the cave, never to return")
              #break out of the while loop
              inCave = false


print("You have fled the cave")