Как обойтись без дублирования кода(Elif) и реализовать посредством функций?
Как обойтись без дублирования кода(Elif) и реализовать по средством функций?
import random
n = 'Not Again!'
while True:
user = input("Enter a choice (rock, paper, scissors, lizard, spoke):\n>>> ")
possible_actions = ["rock", "paper", "scissors" , "lizard" , "spoke"]
answers = ["y" , "n"]
computer = random.choice(possible_actions)
if user not in possible_actions:
print(f'Invalid input "{user}"')
continue
print(f"\nYou chose: {user}\nComputer chose: {computer}\n")
if user == computer:
print(f"Both players selected {user} . Draw!")
elif user == "rock":
if computer == "scissors":
print("You win!")
else:
print("You lose.")
elif user == "paper":
if computer == "rock":
print("You win!")
else:
print("You lose.")
elif user == "scissors":
if computer == "paper":
print("You win!")
else:
print("You lose.")
elif user == "lizard":
if computer == "scissors":
print("You lose.")
else:
print("You win!")
elif user == "spoke":
if computer == "stone":
print("You win!")
else:
print("You lose")
while True:
play_again = input("Play again? (y/n):\n>>>")
if play_again == "y":
break
if play_again not in answers:
print(f'Invalid input "{play_again}"')
continue
if play_again == "n":
print(f'Invalid input "{n}"')
Ответы (1 шт):
Автор решения: needKVAS
→ Ссылка
В первую очередь на ум приходит задание таблицы результатов:
user = input("Enter a choice (rock, paper, scissors, lizard, spoke):\n>>> ")
possible_actions = ["rock", "paper", "scissors" , "lizard" , "spoke"]
results = [[ 0, -1, 1, 1, -1 ], #rock
[ 1, 0, -1, -1, 1 ], #paper
[ -1, 1, 0, 1, -1 ], #scissors
[ -1, 1, -1, 0, 1 ], #lizard
[ 1, -1, 1, -1, 0 ]] #spoke
answers = ["y" , "n"]
computer = random.choice(possible_actions)
if user not in possible_actions:
print(f'Invalid input "{user}"')
continue
print(f"\nYou chose: {user}\nComputer chose: {computer}\n")
index_user = possible_actions.index(user)
index_computer = possible_actions.index(computer)
result = results[index_user][index_computer]
if (result < 0):
print('You lose')
elif (result > 0):
print('You win!')
else:
print('Draw')
continue