Форум сайта python.su
FishHookИз коробки это не отрабатывает. Игрок всегда выигрывает, даже при камне на бумагу и ножницах на камень.
else:
print(“You failed”)
FishHookЗаработало только когда return стал возвращать None.
if mapping == second:
return -1 # second wins
Отредактировано Palrom (Май 24, 2022 17:35:38)
Офлайн
Palromда блин
Заработало только когда return стал возвращать None.
import random mapping = {"к": "б", "н": "к", "б": "н"} def check(first, second): if first == second: return 0 # even if mapping[first] == second: return -1 # second wins return 1 if result == 0: ties += 1 print("Ничья") elif result == 1: wins += 1 print("Победа") else: losses += 1 print("Поражение")
Отредактировано FishHook (Май 24, 2022 17:01:57)
Офлайн
import random mapping = {"к": "б", "н": "к", "б": "н"} def check(first, second): if first == second: return 0 # even if mapping[first] == second: return -1 # second wins return 1 print("КАМЕНЬ, НОЖНИЦЫ, БУМАГА") wins, losses, ties = 0, 0, 0 while True: user_action = input("Выберите: (к)амень, (н)ожницы, (б)умагу, (в)ыход\n") if user_action == "в": exit() comp_action = random.choice(['к', 'н', 'б']) print(comp_action) result = check(user_action, comp_action) if result == 0: ties += 1 print("Ничья") elif result == 1: # Вот теперь работает:) wins += 1 print("Победа") else: losses += 1 print("Поражение") print(f"Побед: {wins} Поражений: {losses} Ничьих: {ties}")
Отредактировано Palrom (Май 24, 2022 17:38:49)
Офлайн
пару замечаний еще позвольте
1.
wins, losses, ties = 0, 0, 0
wins = losses = ties = 0
comp_action = random.choice(['к', 'н', 'б'])
STONE = 1 SCISSORS = 2 PAPER = 3
def get_user_action(): user_action = input("Выберите: (к)амень, (н)ожницы, (б)умагу, (в)ыход\n") if user_action == "в": exit() if user_action = "н": return SCISSORS elif user_action = "б": return PAPER elif user_action = "к": return STONE print("Invalid input") get_user_action()
Отредактировано FishHook (Май 24, 2022 17:56:05)
Офлайн
FishHookНе используй функцию exit() в кодах программ. Она предназначена только для работы в оболочке питона, как help() и dir(). Для выхода из программы существует функция sys.exit() с кодом возврата.if user_action == "в": exit()
Constants added by the site moduleВот эту используй
The site module (which is imported automatically during startup, except if the -S command-line option is given) adds several constants to the built-in namespace. They are useful for the interactive interpreter shell and should not be used in programs.
Отредактировано py.user.next (Май 24, 2022 22:22:05)
Офлайн
Нафуфыренная версия, с функциями:
import random import sys ROCK = 1 SCISSORS = 2 PAPER = 3 def get_user_action(): while True: user_action = input("Выберите: (к)амень, (н)ожницы, (б)умагу, (в)ыход\n") if user_action == "в": return 0 elif user_action == "н": return SCISSORS elif user_action == "б": return PAPER elif user_action == "к": return ROCK else: print("Неверный ввод. Повторите.") continue def get_computer_selection(): selection = random.randint(1, 3) if selection == 1: return ROCK elif selection == 2: return SCISSORS elif selection == 3: return PAPER def get_obj_name(index): if index == 1: return "камень" elif index == 2: return "ножницы" elif index == 3: return "бумага" def determine_winner(user_action, computer_action): mapping = {ROCK: [PAPER], SCISSORS: [ROCK], PAPER: [SCISSORS]} if user_action == computer_action: print(f"Выбор обоих игроков: {get_obj_name(user_action)}.\nНичья!") return 0 print(f"Ваш выбор: {get_obj_name(user_action)}. " f"Выбор оппонента: {get_obj_name(computer_action)}.") if computer_action in mapping[user_action]: print("Вы проиграли.") return -1 else: print("Вы победили!") return 1 def main(): print("КАМЕНЬ, НОЖНИЦЫ, БУМАГА") wins = losses = ties = 0 while True: user_action = get_user_action() if user_action == 0: print('Завершение программы.') sys.exit() computer_action = get_computer_selection() result = determine_winner(user_action, computer_action) if result == 0: ties += 1 elif result == 1: wins += 1 else: losses += 1 print(f"Побед: {wins} Поражений: {losses} Ничьих: {ties}") if __name__ == '__main__': main()
Офлайн
можно ли инпут с двумя параметрами да пжалусто
def input(a, b): print(a, b) input("1", "2")
Отредактировано AD0DE412 (Май 27, 2022 16:42:06)
Офлайн
Palromdef determine_winner(user_action, computer_action): mapping = {ROCK: [PAPER], SCISSORS: [ROCK], PAPER: [SCISSORS]} if user_action == computer_action: print(f"Выбор обоих игроков: {get_obj_name(user_action)}.\nНичья!") return 0 print(f"Ваш выбор: {get_obj_name(user_action)}. " f"Выбор оппонента: {get_obj_name(computer_action)}.") if computer_action in mapping[user_action]: print("Вы проиграли.") return -1 else: print("Вы победили!") return 1
def rps(p1, p2): hand = {'rock':0, 'paper':1, 'scissors':2} results = ['Draw!', 'Player 1 won!', 'Player 2 won!'] return results[hand[p1] - hand[p2]]
Офлайн
Palrom
так оно же не работает
Офлайн
FishHookВсё работает, яж проверял:
так оно же не работает
def rps(p1, p2): hand = {'rock':0, 'paper':1, 'scissors':2} results = ['Draw!', 'Player 1 won!', 'Player 2 won!'] return results[hand[p1] - hand[p2]] print(rps('rock', 'scissors')) # Player 1 won! print(rps('rock', 'rock')) # Draw! print(rps('paper', 'scissors')) # Player 2 won!
Офлайн