0

пишу мини игру на python, есть небольшая проблемка, не могу решить. У меня сейчас разница между всеми уровнями стоит 100 очков. Как сделать так, чтобы каждый раз при достижении уровня очки до след уровня увеличивались на 100 очков? Например 1ур - 100 очков, 2ой уровень - 200 очков, а уже до третьего уровня было 400 очков.

user_score = int(input('Enter your score: '))
level = 0
need_score = 100


while user_score > 0:
    if user_score >= 100:
        user_score -= need_score
        level += 1
        print('your level is: ', level)
        if user_score < 100:
            remain_score = need_score - user_score
            print('to next level need: ', remain_score)
            break
    else:
        level += 1
        remain_score = need_score - user_score
        print('your level is: ', level)
        print('to next level need: ', remain_score)
        break
1
  • 1
    Количество_очков = (2 ** Номер_текущего_уровня) * 100
    – Akina
    20 мар 2019 в 11:36

2 ответа 2

3

Я бы вообще работал только с количеством очков пользователя, из которых можно и уровень вычислить и оставшееся очки до следующего уровня (то есть, очки пользователя просто накапливаются). Исходя из расчета, что между 0 и 1 уровнем - 100 очков, 1 и 2 - 200, 2 и 3 - 400 и т. д.:

import math

user_score = 561

def get_level(score):
    level = int(math.log2(score//100))
    next_level_points = (2**level*100)
    points_left = 2**(level+1)*100 - score
    return level, next_level_points, points_left

res = get_level(user_score)
print(f"User level:{res[0]}\nTotal points to gain next level:{res[1]}\nPoints left to next level:{res[2]}")

Получим:

User level:2
Total points to gain next level:400
Points left to next level:239
0

Как-то так:

user_score = int(input('Enter your score: '))
level = 0
need_score = 100


while user_score > 0:
    if user_score >= need_score: 
        user_score -= need_score
        level += 1
        print('your level is: ', level)
        if level %3 == 0:
            need_score += 200

        else:
            need_score += 100

     if user_score < 100:
            remain_score = need_score - user_score
            print('to next level need: ', remain_score)
            break
     else:
            level += 1
            remain_score = need_score - user_score
            print('your level is: ', level)
            print('to next level need: ', remain_score)
            break

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.