Ошибка в игре рок-ножницы-бумага с Python
Я пытаюсь создать игру «Камень, ножницы, бумага» для задания класса с помощью Python, но не могу получить результат.
Игра должна начинаться с 1) ввода пользователем, 2) показа того, какой компьютер случайно выбрал, 3) результата (вы выигрываете, вы проигрываете или ничья с тем, как вы выиграли или проиграли), 4) затем спросите, хотите ли вы Играйте снова и 5) наконец-то сбегите.
Я добился успеха с первыми двумя частями, но я не могу понять, почему 3, 4, 5 не печатаются ...
В результате после показа того, что компьютер воспроизвел (2), он говорит «Нет», затем возвращается к (1) получение ввода от пользователя.
Ниже приведен код. Посоветуйте, пожалуйста, как их исправить. Большое спасибо!!
import random
while True :
player = input("Enter a number (0 for rock, 1 for paper, 2 for scissors) >>")
RPS = [0,1,2]
computer = random.choics(RPS)
RPS_dict = {0:'Rock', 1:'Paper', 2:'Scissors'}
print("The computer played", RPC_dict.get(computer))
def game(player, computer):
RPS_dict = {0:'Rock', 1:'Paper', 2:'Scissors'}
if player == computer:
print("Tie!")
elif player == 0:
if computer == 1:
print("You lose!", RPS_dict.get(computer), 'covers', RPS_dict.get(player))
else:
print("You win!", RPS_dict.get(player), 'cut', RPS_dict.get(computer))
elif player == 1:
if computer == 2:
print("You lose!", RPS_dict.get(computer), 'cut', RPS_dict.get(player))
else:
print("You win!", RPS_dict.get(player), 'covers', RPS_dict.get(computer))
game(player, computer)
print(game(player, computer))
again = input("Play again? 0 for no, 1 for yes\n")
Ответы
Ваша функция должна находиться за пределами вашего цикла while, и вам не нужно вызывать ее в операторе печати ...
import random
def game(player, computer):
if player == computer:
print('game tied')
else:
if (player == 1 and computer == 3) or (player == 2 and computer == 1) or (player == 3 and computer == 2):
print('you win')
else:
print('computer wins')
def choice(x):
y = None
if x == 1:
y = 'rock'
elif x == 2:
y = 'paper'
elif x == 3:
y = 'scissors'
return y
playing = True
while playing == True:
player_input = int(input("Enter 1 for rock, 2 for paper, 3 for scissors"))
print('You chose:'+ choice(player_input))
computer_input = random.choice([1,2,3])
print('Computer chose:'+ choice(computer_input))
game(player_input, computer_input)
restart = int(input("Enter 1 to play again or 2 to quit"))
if restart == 2:
playing = False