D
D
Dmitry2022-03-10 16:44:13
PHP
Dmitry, 2022-03-10 16:44:13

What is the best way to calculate the level based on experience?

I need to make a level determination based on experience points, but nothing better than this comes to my mind:

def lvl(points: int):
    if points <= 2:
        return 0
    if points <= 5:
        return 1
    if points <= 15:
        return 2


Actually, I hope for the strength of Habr and I'm waiting for suggestions

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
ValeraValera, 2016-05-13
@cluberr

<?php
global $post;
$postslist = get_posts( array( 'posts_per_page' => 2, 'category'=>'news' ) );
foreach ( $postslist as $post ){
  setup_postdata($post);
  ?>
  <div>
    <?php the_date(); ?> - выводит дату новости 
    <?php the_title(); ?> - выводит заголовок новости
    <?php the_excerpt(); ?> - выводит краткое описание 
    <?php the_post_thumbnail(); ?> - выводит превью новости - картинку 
     </div>
  <?php
}
wp_reset_postdata();  ?>

L
LEXXiY, 2016-05-13
@LEXXiY

Either the option above, or use the function https://codex.wordpress.org/Plugin_API/Action_Refe... to change the cycle on the main (well, internal if necessary).
And working with the usual cycle.

I
Igor Markin, 2022-03-10
@Dalaweb

You can implement like this:

RANKS = {
    0: 2,
    1: 5,
    2: 15,
}

def get_rank(points: int) -> int:
    for k, v in RANKS.items():
        if points <= v:
            return k

S
soremix, 2022-03-10
@SoreMix

You can create a regular dictionary, in which the level is the key, and the amount of experience is the value

levels = {0: 2, 1: 5, 2: 15}


def get_level(current_xp):

    sorted_levels = dict(sorted(levels.items()))

    for level, xp in sorted_levels.items():
        if current_xp <= xp:
            return level
    else:
        return list(sorted_levels)[-1]


print(get_level(9))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question