LPTHW Lesson 36

Hey there, having some trouble with creating my own function with a nested if- statement, the rest of my code all runs fine, but whenever i call my function level_up it doesn’t seem to give the desired output.
Thanks in advance for the help

def level_up(x,y):
    if x < 25:
        return y
    elif x >=75:
        return y + 1
    elif x >= 25 or x <= 50:
        return y + 1
xp = 0
level = 1

To begin out quest our hero enters the cave.\nHe is a dragon slayer
to begin he has xp of {xp} and is level {level}\nHis goal is to gain enough xp to slay the dragon
He will need to reach level 3 to do so\nIn the cave there is a tunnel to the left & right
Which way does he go?""")
turn = input('>')

if turn == 'left':
    print("find a pile of gold")
    xp += 25
    print("There is another choice of right/ left tunnels.")
    turn2 = input("hero turns ")

    if turn2 == 'left':
        print("Hits dead end, the princess is eaten")
        turn2 == 'right'
        print("Hears noise ahead through small tunnel")
        print("Stealthily crawls through")
        xp += 15
    print(f"xp = {xp}")
    level = level_up(xp,level)
    print(f"level = {level}")

    turn == 'right'
Find gotlin, it attacks
Hero pulls out sword
does he hit his torso or leg?""")
    leg_torso = input('>')

    if leg_torso == 'leg':
        print("The leg is cut off\nThe hero now slices the stunned goblins head off")
        xp += 25
        leg_torso == 'torso'
        print("The goblin bloks the blow.\nNext the hero hits the goblins unshielded head, killing him")
        xp += 15
    print(f"xp = {xp}")
    level = level_up(xp,level)
    print(f"level = {level}")

inside the next tunnel in a goblin horde with their backs turned
Does our hero: shoot an arrow at the leader? or Blow his horn?""")
sneak_attack = input('>')

if 'arrow' in sneak_attack:
    print("The leader is killed, all the goblins run away")
    xp += 35
    'horn' in sneak_attack
    print("all the goblins run away")
    xp += 5

print(f"xp = {xp}")
level = level_up(xp,level)
print(f"level = {level}")
our hero now enters the Final cavern and sees the princess in a cage
Then the giant dragon runs out to protect her""")

if level >= 3:
Our hero hefts his spear, killing the dragon.\nThe princess is saved!!")
He unlcoks her cage & gets a smooch""")
    level <= 2
    print("Our hero hefts his spear, but is too slow")
    print("The dragon breathes his flame, killing the hero")
    print("game over")

Hello @Dankmustard

You are comparing 1 to y instead of assigning 1 to y.

Use only one ”=” for assigning.
Two for comparison.

1 Like

You can post code like this:

# your pythong here


# your python here

Can you delete your images and then reformat your original post? Let me know if you can’t.

1 Like

Thanks, got it.

I was also able to tinker with my code a bit and get it running.

found i wasn’t re-assigning my function to the variable level:
before: level_up(xp,level)
after: level = level_up(xp,level)

and i also changed the while loop at the beginning & did what ulfen69 recommended.

Would there have been a way to do that with a while loop?
Or does that not make sense, as i only need to run through that function once every time i call it?

Great, looks like you’re on the path then.