Answer the question
In order to leave comments, you need to log in
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
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
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
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 questionAsk a Question
731 491 924 answers to any question