Galaxycrasher 38
Galaxycrasher 38

Reputation: 3

Trying to make a leveling system, however it only works once and then stops working?

I'm making a leveling system and it only levels me up once and then stops working. Once it levels me the xp doesn't reset and my level does not go up. Here's the code!

level = int(1)
crexp = int(260)
reqxp = int(100)
while crexp >= reqxp:
    level = level+1
    crexp = crexp-reqxp
    reqxp = (reqxp/100)*120
    continue
while 3 > 2:
    pinput = input()
    if pinput == "1":
        crexp = crexp + 60
    elif pinput == "2":
        print(level)
    elif pinput == "3":
        print(crexp)
    elif pinput == "4":
        print(reqxp)
    elif pinput == "5":
        break

Upvotes: 0

Views: 56

Answers (1)

Daveeeee101
Daveeeee101

Reputation: 26

The problem with your current code is that you are not rerunning the 'level up' part of the code. Python generally (when not in a while/for loop e.c.t) reads your code from top to bottom. This means by the time you get into the second while loop the first while loop has finished and will never be run again. To fix this you want to tell python to recalculate the level and experience variables at certain points - the easiest way to do this is to make the first while loop into a function and call it at the start of the second while loop. You would get something like this -

def checkLevelUp(currentXp, requiredXp, currentLevel):
    while currentXp >= requiredXp:
        currentLevel = currentLevel+1
        currentXp = currentXp-requiredXp
        requiredXp = int(requiredXp * 1.2)
    return currentLevel, currentXp, requiredXp


level = 1
crexp = 260
reqxp = 100


while True:
    level, crexp, reqxp = checkLevelUp(crexp, reqxp, level)
    pinput = input()
    if pinput == "1":
        crexp = crexp + 60
    elif pinput == "2":
        print(level)
    elif pinput == "3":
        print(crexp)
    elif pinput == "4":
        print(reqxp)
    elif pinput == "5":
        break

Note also the changes to calculating the next required xp - dividing by 100 and then multiplying by 120 is just the same as multiplying by 1.2.

Upvotes: 1

Related Questions