M
M
mikhal_ivanych2020-06-11 19:51:38
Python
mikhal_ivanych, 2020-06-11 19:51:38

Is the darts scoring problem solved correctly?

Hello.
Solved the problem. To whom it is not difficult - please write down if there are errors / shortcomings.
Thank you.

"""
Написать функцию для подсчета очков при игре в дартс.
Правила начисления очков:
Радиус больше 10 - 0 очков
Радиус между 5 и 10 включительно - 5 очков
Радиус менее 5 - 10 очков
Если все радиусы менее 5, начисляются дополнительные 100 очков.
"""

min_points = 0
mid_points = 5
max_points = 10
bonus_points = 100


def calculate_score(lst):
    total_score = 0

    for i in lst:
        if i < 5:
            total_score += max_points
        else:
            for j in lst:
                if 5 <= j <= 10:
                    total_score += mid_points
                else:
                    total_score += min_points
            return total_score
    return total_score + bonus_points


print(calculate_score([1, 5, 11]))
print(calculate_score([15, 20, 30]))
print(calculate_score([1, 2, 3, 4]))

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2020-06-11
@mikhal_ivanych

You don't need to do many cycles. If there are more than 3 throws, your code will not process them, you can check.
I would do so

def calculate_score(lst):
    total_score = 0
    has_bonus = True

    for radius in lst:
        if radius >=5 and radius <=10:
            total_score += mid_points
        
        elif radius < 5:
            total_score += max_points

        if radius > 5:
            has_bonus = False

    if has_bonus:
        total_score += bonus_points

    return total_score

Or you can check for a bonus using the max(lst) function, it will return the largest number in the array, respectively, if the maximum number is greater than 5, there will be no bonus
def calculate_score(lst):
    total_score = 0

    for radius in lst:
        if radius >=5 and radius <=10:
            total_score += mid_points
        
        elif radius < 5:
            total_score += max_points

    if max(lst) < 5:
        total_score += bonus_points

    return total_score

M
mikhal_ivanych, 2020-06-11
@mikhal_ivanych

SoreMix yes, it looks like the max(lst) option looks better and there is one cycle, not two. Plus you removed the 0 option...
Thanks for the comment.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question