Answer the question
In order to leave comments, you need to log in
How to simplify this python code?
Hello.
It was sad and lonely, so I wrote a rock-paper-scissors game. In the course of the solution, the following construction arose:
def whowin (first, second):
if first == 'ножницы' and second == 'камень':
print("second win")
elif first == 'камень' and second == 'бумага':
print('second win')
elif first == 'бумага' and second == 'ножницы':
print('second win')
elif first == 'ножницы' and second == 'бумага':
print('first win')
elif first == 'камень' and second == 'ножницы':
print('first win')
elif first == 'бумага' and second == 'камень':
print("first win")
Answer the question
In order to leave comments, you need to log in
1 - ножницы
2 - бумага
3 - камень
def whowin (first, second):
result = (second - first) if abs(second - first) != 2 else -(second - first)
if result == 0:
print("ничья")
elif result > 0:
print('first win')
else:
print('second win')
def whowin (first, second):
result = (second - first) if abs(second - first) != 2 else -(second - first)
print("first win" if result > 0 else "second win")
def whowin(first, second):
game = {('ножницы', 'камень'): "second win",
('камень', 'бумага'): 'second win',
('бумага', 'ножницы'): 'second win',
('ножницы', 'бумага'): 'first win',
('камень', 'ножницы'): 'first win',
('бумага' 'камень'): 'first win'}
print(game[(first, second)])
def whowin (first, second):
if any([
all([first == 'ножницы',second == 'камень']),
all([first == 'камень',second == 'бумага']),
all([first == 'бумага',second == 'ножницы']),
]):
return ('second win')
elif any([
all([first == 'ножницы',second == 'бумага']),
all([first == 'камень',second == 'ножницы']),
all([first == 'бумага',second == 'камень']),
]):
return ('first win')
else:
return None
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question