Python Forum

Full Version: Simple code help needed
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm quite new to Python, and need help with a simple code.
------------------------------------------------------------------------------------
axe = False
sword = False
Here are the starting variables for wether user has an axe or a sword.
------------------------------------------------------------------------------------
if q2a == "1":
        sword = True

if q2a == "2":
        axe = True
Here is where they are changed to True, to indicate that user has one.
------------------------------------------------------------------------------------
       if sword == True:
            print("You slash off the wolves head before it attacks you, killing it.")
            print("You have,",health,"remaining")
        elif axe == True:
            print("The wolf bites you once and you swing your axe at the wolf, cutting it clean in half.")
            health = health - 20
            print("You have,",health,"remaining")
        else:
            print("Your bare hands were not strong enough, and you were killed. Returning to menu!")
Here, even if sword or axe is set to True, it always ends up with:
"Your bare hands were not strong enough, and you were killed. Returning to menu!".
Could someone help out?
Are you using functions? You could have more than one variable named sword or axe depending on what the current scope is.
Here, I'll just post the whole code so you can see. I don't really understand much but I just wanted tot try doing something
axe = False
sword = False
health = 100
def begin():
    print("Game starting!\n\n")
    q1()

def q2a():
    q2a=input("You walk to the chest. You find that the chest is empty, but the skeleton has a sword and an axe but you can only hold one.\nDo you want to take the sword(1)?, or the axe(2)")
    if q2a == "1":
        sword = True
        print("Sword was taken")
        q2b()

    if q2a == "2":
        axe = True
        print("Axe was taken")
        q2b()
        
def q2b():
    q2b=input("You are attacked by a wolf that jumps out of the trees nearby!\nDo you want to fight(1)?, or run(2)?")
    print(sword)
    print(axe)
    if q2b == "1":
        
        if sword == True:
            print("You slash off the wolves head before it attacks you, killing it.")
            print("You have,",health,"remaining")
        elif axe == True:
            print("The wolf bites you once and you swing your axe at the wolf, cutting it clean in half.")
            health = health - 20
            print("You have,",health,"remaining")
        else:
            print("Your bare hands were not strong enough, and you were killed. Returning to menu!")
            menu()
    if q2b == "2":
        print("You are chased and killed by the wolf. Returning to menu!")
        menu()

def q1():
    q1=input("You have woken up in the middle of nowhere with nothing but your clothes. You see an open chest with a skeleton nearby.\nDo you want to check out the chest(1) or walk the other way?(2)\n")
    if q1 == "1":
        print("You have",health,"health") 
        q2a()
    if q1 == "2":
        print("You have",health,"health") 
        q2b()


def menu():
    print("""
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓
▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓
▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓Stranded island game▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓
▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓
▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓
▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓
▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓Play?            Y/N▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓
▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓
▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
""")
    play=input()
    if play == "Y":
        begin()
    elif play == "N":
        print("Bye!")
    else:
        print("Enter a valid choice")
    

menu()
(Feb-07-2018, 08:39 PM)Harveyyy Wrote: [ -> ]
def q2a():
    q2a=input("You walk to the chest. You find that the chest is empty, but the skeleton has a sword and an axe but you can only hold one.\nDo you want to take the sword(1)?, or the axe(2)")
    if q2a == "1":
        sword = True
        print("Sword was taken")
        q2b()
 
    if q2a == "2":
        axe = True
        print("Axe was taken")
        q2b()

In this function, you create two new variables, sword and axe, which happen to have the same name as variables in the global scope. This is called "shadowing". https://en.wikipedia.org/wiki/Variable_shadowing

You can fix that in one of two ways:
1) Restructure your program to avoid using globals. This is the better option, but would involve rewriting everything.
2) Add the line global sword, axe to the top of the function, to hint to python that you're referring to those variables in a different scope.