Помогите мне сжать код python
Кажется, у меня получился довольно большой код. и я хочу его уменьшить:
if result_ >= 1000 and result_ <= 1999:
print("bronze1")
else:
if result_ >= 2000 and result_ <= 2999:
print("bronze2")
else:
if result_ >= 3000 and result_ <= 3999:
print("bronze3")
else:
if result_ >= 4000 and result_ <= 4999:
print("silver1")
else:
if result_ >= 5000 and result_ <= 5999:
print("silver2")
else:
if result_ >= 6000 and result_ <= 6999:
print("silver3")
else:
if result_ >= 7000 and result_ <= 7999:
print("gold1")
else:
if result_ >= 8000 and result_ <= 8999:
print("gold2")
else:
if result_ >= 9000 and result_ <= 9999:
print("gold3")
else:
if result_ >= 10000 and result_ <= 10999:
print("diamond1")
else:
if result_ >= 11000 and result_ <= 11999:
print("diamond2")
else:
if result_ >= 12000 and result_ <= 12999:
print("diamond3")
else:
if result_ >= 13000 and result_ <= 13999:
print("mythic1")
else:
if result_ >= 14000 and result_ <= 14999:
print("mythic2")
else:
if result_ >= 15000 and result_ <= 15999:
print("mythic3")
else:
if result_ >= 16000 and result_ <= 16999:
print("legendary1")
else:
if result_ >= 17000 and result_ <= 17999:
print("legendary2")
else:
if result_ >= 18000 and result_ <= 18999:
print("legendary3")
else:
if result_ >= 19000:
print("master")
Ответы (3 шт):
Автор решения: MBo
→ Ссылка
Для случая равномерного шага решение может быть довольно простым:
treasures = ["", "bronze1", "bronze2", "bronze3", "silver1" и т.д....]
print(treasures[min(19, result//1000)])
Автор решения: Stanislav Volodarskiy
→ Ссылка
table = (
( 1000, "bronze1" ), ( 2000, "bronze2" ), ( 3000, "bronze3" ),
( 4000, "silver1" ), ( 5000, "silver2" ), ( 6000, "silver3" ),
( 7000, "gold1" ), ( 8000, "gold2" ), ( 9000, "gold3" ),
(10000, "diamond1" ), (11000, "diamond2" ), (12000, "diamond3" ),
(13000, "mythic1" ), (14000, "mythic2" ), (15000, "mythic3" ),
(16000, "legendary1"), (17000, "legendary2"), (18000, "legendary3"),
(19000, "master" )
)
for value, grade in reversed(table):
if result_ >= value:
print(grade)
break
Автор решения: Amgarak
→ Ссылка
Как ещё один вариант:
import bisect
my_elo = 18943
grades = {
1000 : "bronze1",
2000 : "bronze2",
3000 : "bronze3",
4000 : "silver1",
5000 : "silver2",
6000 : "silver3",
7000 : "gold1",
8000 : "gold2",
9000 : "gold3",
10000 : "diamond1",
11000 : "diamond2",
12000 : "diamond3",
13000 : "mythic1",
14000 : "mythic2",
15000 : "mythic3",
16000 : "legendary1",
17000 : "legendary2",
18000 : "legendary3",
19000 : "master"
}
rank_elo, rank = zip(*grades.items())
index = bisect.bisect_right(rank_elo, my_elo) - 1
if index >= 0:
print(rank[index])
else:
print("У вас нету ранга!")
Вывод:
legendary3
[Program finished]