练级系统python无法正常工作

时间:2018-01-19 14:04:36

标签: python

if "comb" not in inventory:
            print ("You went over to the table and picked up the comb,")
            print ("it's been added to your inventory.")
            add_to_inventory("comb")
            print("")
            print ("Inventory: " + str(inventory))
            lvl = 1
            xp = 0
            lvlNext = 50

            xp= xp+10

            while xp >= lvlNext:
                lvl += 1
                xp = xp - lvlNext
                lvlNext = round(lvlNext * 1.5)

            print ('level', str(lvl))
            print ('Exp:', str(xp))
            print ('Next:', str(lvlNext))

当我在python中运行它时,没有错误,但它没有给我我想要的东西。这是我收到的结果:

等级1 Exp:10 下一个:50

这是我想要的结果:

等级1 Exp:10 下一篇:40

我不确定我的代码中出错的位置。

2 个答案:

答案 0 :(得分:0)

你想要的是do while循环 - 做一件事,检查你是否需要再做一次。虽然Python没有直接do-while,但您可以创建一个:

while True:
#do something first

if condition_to_break: #check if we don't need to do it again if so break.
    break

这对于练级系统非常有用,在这个系统中你可以获得比你需要的水平更多的xp。举例来说,我们获得100 xp但只需要50级:

xp = 10
levelNext = 50
lvl = 1
while True:
    levelNext = levelNext - xp 
    if levelNext > 0: #meaning we didn't have enough to level 
        xp = 0
    else: # meaning we have more xp than we need to level
        xp = abs(levelNext) #take the remainding amount of xp and take the positive number for it
        lvl += 1
        levelNext = round((lvl+50) * 1.5) #gets the next tier of lvlNext since if we hit this then it means we've leveled up 
                                          #should use the current lvl to calculate the next tier of exp you need since levelNext is changing
    if xp <= 0: #this breaks out when we ran out of xp.
        break

运行上面的代码将使你仍然保持1级,剩下0 xp和40 LevelNext。如果你将xp更改为100,那么你将是2级,剩下0 xp,28个LevelNext点。

旁注,因为它代表你的等级和exp以及每次你拿起comb时不会重置的内容。如果这不是您想要的效果,您可能希望将您的级别设置在if语句之外。

答案 1 :(得分:-1)

此处控件根本没有进入while循环。 只需改变你的状态: 而xp&lt; = lvlnxt