调平系统不工作

2024-04-19 07:49:48 发布

您现在位置: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中运行这个程序时,没有出现错误,但是它没有给出我想要的东西。这是我收到的结果:

一级 实验:10 下一个:50

这就是我想要的结果:

一级 实验:10 下一个:40

我不确定我在代码中哪里出错。你知道吗


Tags: thetoinyouifnotxpover
2条回答

在这里,控件根本没有进入while循环。 只要改变你的状态: 当xp<;=lvlnxt时

您需要的是一个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

这对于一个升级系统很有用,在这个系统中你可以获得比你需要升级更多的经验。举个例子,我们获得100点经验值,但只需要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。如果你把经验值改为100,那么你将是2级,剩下0点经验值,下一级28点。你知道吗


一个旁注,因为它代表你的水平和经验,什么不重置每次你拿起一个comb。如果这不是期望的效果,您可能需要在if语句之外设置级别。你知道吗

相关问题 更多 >