Search code examples
if-statementoperators

Condition being ignored?


So, I'm relatively new to Python and I apologise in advance if this question seems dumb.

In the practice game I am attempting to make to understand how to make my own text-based adventure, the player is asked if they will proceed and they must use their lantern to advance further down a tunnel, subsequently saying this;

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 ("")

If their lantern is not on, it will print so with;

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?")

The statements are defined above within a separate while loop;

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

The player should continue when their lantern is lit with this;

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?")

My issue however, is when the player tries to use any other answer than "n" when their lantern is lit, what will be printed is as though their lamp hasn't been used.

Does anyone know why this could be and do I need to be more specific? Thanks and again, sorry if this a stupid question.


Solution

  • I wasn't able to follow what you want from the code, but hopefully below gives you some ideas to follow. Specifically you ask "any other answer than "n" when their lantern is lit [but it prints] as though their lamp hasn't been used". The only answer provided that is not "no" is the "use lantern option", but that option checks if the player has no lamp. And the other statements will only print if you do answer "no". So it seems to me that the part of the code that is printing is not included in the question.

    I tried to keep it simple to follow along and maybe it can give some clues! I had fun writing it anyway.

    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")