Python Text Adventure Boolean issue



  • I cannot seem to figure out why my Boolean values will not change after going straight. I just decided to start coding less than aweek ago. Can someone help me figure out why these values will not change?```

    def left():
        print("you hit a dead end. Do you want to go back?")
    
    
    def right():
        print('You are encountered by a giant spider. Do you want to fight it?')
    
    
    def straight():
        print('You find a chess with a sword inside of it. Do you want to take it?')
    
    
    
    start_game = input('Type [Start] to begin or [Quit] to end:')
    
    user_has_sword = False
    sword_in_chess = True
    
    
    while start_game.lower() == 'start':
        direction = input("""   
    You find yourself in a dark room with three ways to go. 
    Which way do you want to go? [Left, Right, Straight]""")
    
        if direction.lower() == "left":
            print("you hit a dead end. Do you want to go back?")
            answer = input('> ')
            if answer == 'yes':
                continue
            else:
                print('You bring shame')
                break
    
        elif direction.lower() == 'right':
            print(right())
            answer = input('> ')
            if answer == "yes" and user_has_sword == True:
                print('You won!')
                break
            elif answer == 'yes'and user_has_sword != True:
                print('You died')
                break
            else:
                print('You go back to the main room')
                continue
    
        elif direction.lower() == 'straight':
            print(straight())
            take_sword = input('> ')
            if take_sword == 'yes':
                user_has_sword = True,
                sword_in_chess = False
                print('The sword has been added to your inventory and you go back.')
                continue
            elif user_has_sword is True and sword_in_chess is False:
                print('You already have this item')
                continue
    
            else:
                print('You go back')
                continue
    
        elif answer != 'left' or 'right' or 'straight':
            print('Invalid option')
    
    else:
        print('Bye!')
    


  • I think you have a logical error. You are setting

    user_has_sword = True,
    sword_in_chess = False
    

    in

    elif direction.lower() == 'straight':
    

    everytime the user enters 'yes' to picking up the sword.

    You can add another conditional statement either after or before to check if the sword has already been picked up and or is not in the chess anymore

    if take_sword == 'yes':
    

    One way the logic could like like the following:

    ...
    elif direction.lower() == 'straight':
            print(straight())
            take_sword = input('> ')
            if take_sword == 'yes':
             if user_has_sword is False and sword_in_chess is
             True:
               user_has_sword = True,
               sword_in_chess = False
               print('The sword has been added to 
               your inventory and you go back.')
               continue
             elif user_has_sword is True and 
             sword_in_chess is False:
                print('You already have this item')
                continue
        ...
    

    Let me know if this is this unclear and I can try to clarify.



  • I think you have a logical error. You are setting

    user_has_sword = True,
    sword_in_chess = False
    

    in

    elif direction.lower() == 'straight':
    

    everytime the user enters 'yes' to picking up the sword.

    You can add another conditional statement either after or before to check if the sword has already been picked up and or is not in the chess anymore

    if take_sword == 'yes':
    

    One way the logic could like like the following:

    ...
    elif direction.lower() == 'straight':
            print(straight())
            take_sword = input('> ')
            if take_sword == 'yes':
             if user_has_sword is False and sword_in_chess is
             True:
               user_has_sword = True,
               sword_in_chess = False
               print('The sword has been added to 
               your inventory and you go back.')
               continue
             elif user_has_sword is True and 
             sword_in_chess is False:
                print('You already have this item')
                continue
        ...
    

    Let me know if this is this unclear and I can try to clarify.



  • That did it. Can you explain why it needed a nested if statement?



  • @4lph40mega Sure.

    let's put it in pseudo code.

    if direction is straight
     print straight message e.g. "Do you want to pick up sword?"
     if user DOES NOT have sword and sword IS in chess
      user has sword now & 
      remove sword from chess
     else if user HAS sword and sword IS NOT in chess
      print "You have already picked up sword"
     end nested if
    end first if
     
    

    All we are doing is making sure that user does not have the sword before we change
    user_has_sword = True,
    sword_in_chess = False
    Otherwise we don't do anything except print "You have already picked up sword"

    Keep in mind the solution above is probably not the most optimal solution.
    One way you can improve it is by changing the second nested conditional statement from

    elif user_has_sword is True and 
             sword_in_chess is False:
    

    to just

    else: 
    

    Another way to improve is to check if the sword is in the chess before you you ask if they want to pick up the sword. But that has more to do with how you want to design the game and not necessarily if the code is correct.

    Hope this helps. Do you mind selecting my answer as the correct answer if it worked for you? Thank you!


Log in to reply