Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dice.py #3587

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 69 additions & 2 deletions Module8/practice/Dice/dice.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,73 @@
import random

class Dice:
# Тут будет код игрального шестигранного кубика
pass
def __init__(self, num_sides=6):
self.num_sides = num_sides
self.__side = None

def roll(self):
self.__side = random.randint(1, self.num_sides)

# геттер
@property
def side(self):
return self.__side

# сеттер
@side.setter
def side(self, new_value):
if 1 <= new_value <= self.num_sides:
self.__side = new_value
else:
raise ValueError("Недопустимое значение грани")

def __lt__(self, other):
if not isinstance(other, Dice):
raise TypeError("Невозможно сравнить кубик с другим типом данных")
if self.__side is None or other.side is None:
raise ValueError("Сначала нужно подбросить кубики")
return self.__side < other.side

def __gt__(self, other):
if not isinstance(other, Dice):
raise TypeError("Невозможно сравнить кубик с другим типом данных")
if self.__side is None or other.side is None:
raise ValueError("Сначала нужно подбросить кубики")
return self.__side > other.side

def __eq__(self, other):
if not isinstance(other, Dice):
raise TypeError("Невозможно сравнить кубик с другим типом данных")
if self.__side is None or other.side is None:
raise ValueError("Сначала нужно подбросить кубики")
return self.__side == other.side

num_dice = int(input("Введите количество кубиков: "))
dice_list = []
for i in range(num_dice):
num_sides = int(input(f"Введите количество граней для кубика {i+1}: "))
dice = Dice(num_sides)
dice.roll()
dice_list.append(dice)

for i, dice in enumerate(dice_list):
counts = [d.side for d in dice_list].count(dice.side)
print(f"{dice.side}-ка выпала у {counts} кубиков")

for i in range(num_dice):
for j in range(i+1, num_dice):
try:
if dice_list[i] < dice_list[j]:
print(f"Кубик {i+1} меньше кубика {j+1}")
elif dice_list[i] > dice_list[j]:
print(f"Кубик {i+1} больше кубика {j+1}")
elif dice_list[i] == dice_list[j]:
print(f"Кубик {i+1} равен кубику {j+1}")
except TypeError as e:
print(e)
except ValueError as e:
print(e)


# TODO-1: Найти некорректные значение, которые можно записать в атрибут .side и исправьте

Expand Down