如何根据经验确定等级?

时间:2017-12-06 20:51:53

标签: python python-3.x

如何根据输入体验简单地计算水平? 我正在为一款名为runescape的游戏制作一个“宠物机会”。该程序会询问您当前的体验,并根据用户输入的体验值来近似级别。下面是我能想到的唯一方法,但正如你所看到的那样,有一面文字基本上是复制粘贴了40次。请帮忙!

我目前有:

def calculate_level(xp):
if xp < 368599:
    level = 62
elif xp < 368599 * 1.101141**2:
    level = 63
elif xp < 368599 * 1.101141**3:
    level = 64
elif xp < 368599 * 1.101141**4:
    level = 65
elif xp < 368599 * 1.101141**5:
    level = 66
elif xp < 368599 * 1.101141**6:
    level = 67
elif xp < 368599 * 1.101141**7:
    level = 68
elif xp < 368599 * 1.101141**8:
    level = 69
elif xp < 368599 * 1.101141**9:
    level = 70
elif xp < 368599 * 1.101141**10:
    level = 71
elif xp < 368599 * 1.101141**11:
    level = 72
elif xp < 368599 * 1.101141**12:
    level = 73
elif xp < 368599 * 1.101141**13:
    level = 74
elif xp < 368599 * 1.101141**14:
    level = 75
elif xp < 368599 * 1.101141**15:
    level = 76
elif xp < 368599 * 1.101141**16:
    level = 77
elif xp < 368599 * 1.101141**17:
    level = 78
elif xp < 368599 * 1.101141**18:
    level = 79
elif xp < 368599 * 1.101141**19:
    level = 80
elif xp < 368599 * 1.101141**20:
    level = 81
elif xp < 368599 * 1.101141**21:
    level = 82
elif xp < 368599 * 1.101141**22:
    level = 83
elif xp < 368599 * 1.101141**23:
    level = 84
elif xp < 368599 * 1.101141**24:
    level = 85
elif xp < 368599 * 1.101141**25:
    level = 86
elif xp < 368599 * 1.101141**26:
    level = 87
elif xp < 368599 * 1.101141**27:
    level = 88
elif xp < 368599 * 1.101141**28:
    level = 89
elif xp < 368599 * 1.101141**29:
    level = 90
elif xp < 368599 * 1.101141**30:
    level = 91
elif xp < 368599 * 1.101141**31:
    level = 92
elif xp < 368599 * 1.101141**32:
    level = 93
elif xp < 368599 * 1.101141**33:
    level = 94
elif xp < 368599 * 1.101141**34:
    level = 95
elif xp < 368599 * 1.101141**35:
    level = 96
elif xp < 368599 * 1.101141**36:
    level = 97
elif xp < 368599 * 1.101141**37:
    level = 98
else:
    level = 99
return level

3 个答案:

答案 0 :(得分:5)

闻起来像对数..

import math

def xp_to_level(xp):
    return math.ceil(math.log(xp/368599, 1.101141)) + 61

用法:

>>> xp_to_level(123456)
50

答案 1 :(得分:0)

你能简化这一点的一个坏方法就是这个

def calculate_level(xp):
    if xp < 368599:
        return 62
    for i in reversed(range(63, 99)):
        if xp > 368599 * 1.101141**(i - 62):
            return i

但另一个更好的办法是找出这个问题的更多数学定义,并找出直接函数。

我认为这将是

的内容
level = floor(log_1.101141(xp / 368599) +62)

答案 2 :(得分:-1)

这是一种方法(不是完整的解决方案,只是一个例子)

from collections import OrderedDict
import io

# Put your levels in a csvfile

data = '''\
62,144815767.41229948
63,159462585.01260683
64,175590796.3918269
65,193350231.1981527
66,212905873.000225
67,234439391.96980074
68,258150832.58147836
69,284260472.00806165
70,313010866.47588897
71,344669104.59058684'''

file = io.StringIO(data)

# Read file to dictionary
L = OrderedDict((int(i.split(',')[0]),float(i.split(',')[1])) for i in file.read().split('\n'))

experience = 450000

# Now to the code
current_lvl = next(k for k,v in L.items() if v > experience)

返回62。现在您有了字典,您可以执行以下操作:

计算下一级所需的exp:

L.get(current_lvl+1)-experience

返回159012585.01260683