如何在dict中调用下一个值?

发布于 2025-01-23 14:00:14 字数 1027 浏览 0 评论 0原文

i used the dict

LEVEL_XP = {
   0: 0,
   1: 5,
   2: 13,
   3: 20
}

and the code

new_level = int(level)
for level_on, xp_needed in LEVEL_XP.items():
    i = LEVEL_XP[xp_needed]

    if 0 <= i:
       if xp != i:
          print('nothing')
          break
       else:
          new_level = int(level_on) + 1
          print('level up')
          break

    else:
       new_level = int(level)
       break

where i could call level and xp from database and it works fine. the problem is after i reach 13 xp, i dont level up to 2 and its printing nothing. i've found the reason that the xp_needed calls only 0 which is the first value in the dict. 如何获得代码考虑下一个值,该值是13在我级别1时? 以及如果我喜欢将整个XP删除至0并使用级别1重新启动,则该怎么办,因此代码需要调用13xp_needed接下来,不是5

i used the dict

LEVEL_XP = {
   0: 0,
   1: 5,
   2: 13,
   3: 20
}

and the code

new_level = int(level)
for level_on, xp_needed in LEVEL_XP.items():
    i = LEVEL_XP[xp_needed]

    if 0 <= i:
       if xp != i:
          print('nothing')
          break
       else:
          new_level = int(level_on) + 1
          print('level up')
          break

    else:
       new_level = int(level)
       break

where i could call level and xp from database and it works fine.
the problem is after i reach 13 xp, i dont level up to 2 and its printing nothing.
i've found the reason that the xp_needed calls only 0 which is the first value in the dict.
How can i get the code consider next value which is 13 in the dict when i am level 1?
and How to do if i like erase the whole xp to 0 and restart it with level 1, so the code need to call 13 as xp_needed next, not 5?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬花如无物 2025-01-30 14:00:14

如何获得代码考虑下一个值...?

在下面使用循环时使用
It takes advantage of experience being monotonic with level.
We can probe each [level + 1] element quite efficiently,
without having to visit the "tons" of levels you mentioned.
We only examine levels that could be relevant.

class Character:

    def __init__(self, level=0, xp=0):
        self.level = level
        self.xp = xp

    def __str__(self):
        return f'<Character: level {self.level}, xp {self.xp}>'

    def adjust_to_increased_experience(self, new_xp):
        assert new_xp >= self.xp
        assert new_xp >= LEVEL_XP[self.level]
        while LEVEL_XP.get(self.level + 1, math.inf) <= new_xp:
            self.level += 1
        self.xp = new_xp

How can i get the code consider next value ... ?

Use the while loop below.
It takes advantage of experience being monotonic with level.
We can probe each [level + 1] element quite efficiently,
without having to visit the "tons" of levels you mentioned.
We only examine levels that could be relevant.

class Character:

    def __init__(self, level=0, xp=0):
        self.level = level
        self.xp = xp

    def __str__(self):
        return f'<Character: level {self.level}, xp {self.xp}>'

    def adjust_to_increased_experience(self, new_xp):
        assert new_xp >= self.xp
        assert new_xp >= LEVEL_XP[self.level]
        while LEVEL_XP.get(self.level + 1, math.inf) <= new_xp:
            self.level += 1
        self.xp = new_xp
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文