Я новый программист, работаю над своей первой игрой. Это простой камень, ножницы, бумага, против компьютера, но, к сожалению, он не распечатывает, кто победит. Лог c состоит в том, что компьютер выбирает случайным образом 0,1 или 2, а затем распечатывает камень, бумагу или ножницы в зависимости от массива и их выбора, так что это «случайный». Затем пользователь вводит, выберет ли он камень, бумагу или ножницы, а затем следует выбрать победителя с помощью операторов if, elif, else. Я тестировал его так:
rock = RockPaperScissors()
print(rock.game_play())
Вот фактический код игры:
import random
class RockPaperScissors():
global rps
rps = ['rock', 'paper','scissors']
def computer_choice(self): #computer randomly picks rock paper or scissors
x = random.randint(0,2)
w = rps[x]
return "the computer chooses: " + w
return 'the computer chooses {f}'.format(f= rps[x])
def player_choice(self):
x = input("Player- would you like rock, paper, or scissors? enter rock,paper, or scissors?: ").lower()
while x != 'rock' and x != 'paper' and x != 'scissors':
choice = (input("Player- would you like rock, paper, or scissors? enter rock, paper, or scissors?: "))
x = input("please enter in rock, paper or scissors").lower()
return x
def game_play(self):
rock = RockPaperScissors()
user = rock.player_choice()
#print(rock.player_choice())
comp= rock.computer_choice()
return (comp)
game = True
r = 'rock'
p = 'paper'
s = 'scissors'
while game:
if comp == p and user == r:
return "the player wins!"
game = False
elif comp == s and user == r:
return "the computer wins!"
elif comp == r and user == p:
return "the computer wins"
elif comp == s and user == p:
return "the player wins!"
elif comp == p and user == s:
return"the computer wins!"
elif comp == r and user == s:
print( "the player wins" )
elif comp == user:
print('its a tie')
Если пользователь выбирает камень, а компьютер случайный, вот пример вывода:
Игрок, ты хочешь камень, бумагу или ножницы? ввести камень, ножницы или бумагу ?: камень, выбранный компьютером: ножницы
как видите - победитель не определяется.
Спасибо за любую помощь! приветствуются любые другие улучшения! :)