#python #loops #while-loop
Вопрос:
когда я запускаю программу, она зацикливается снова и снова, даже если game rounds
это равно rounds
пробованным различным способам, но все равно подслушивает или есть что-то, чего я не могу увидеть
вот код:
print('Rock Paper Scissors!n"r" for rock, "p" for paper, "s" for scissorsn')
rounds = int(input('How many rounds?: '))
game_round = 0
game = game_round != rounds
while game:
user = input('nRock, Paper, Scissors?: ').lower()
computer = random.choice(['r', 'p', 's'])
print(f'Computer: {computer}n')
player = user
opponent = computer
user_win = 0
# r > s, s > p, p > r
# if player wins:
player_win = (player == 'r' and opponent == 's') or (player == 's' and opponent == 'p') or (player == 'p' and opponent == 'r')
# if computer wins:
computer_w = (opponent == 'r' and player == 's') or (opponent == 's' and player == 'p') or (opponent == 'p' and player == 'r')
if user == computer and game:
game_round = game_round 1
print('Tie!')
if player_win and game:
user_win = user_win 1
game_round = game_round 1
print('You win!')
if computer_w and game:
game_round = game_round 1
print('You loose!')
if game is False:
print(f'Score {user_win}/{rounds}')```
Комментарии:
1. куда вы направляетесь
false
game
?2.
game
Переменная бессмысленна, просто установите цикл while в состояниеwhile game_round != round
Ответ №1:
Как говорит Сайс в комментарии, вы можете сделать вот так:
user_win = 0 # <- put user_win outside the loop so you can access it outside the loop.
while game_round != round:
# other code
print(f"Score {user_win}/{rounds}")