Не могу выйти из игры l oop после повтора (пока l oop) - PullRequest
0 голосов
/ 11 января 2020
# Rock paper Scissors

import random


# Setting computer to choose random numbers from 1-3

def computer_option():

    computer = random.randint(1,3)

    if computer == 1:
        computer = 'r'
    elif computer == 2:
        computer = 'p'
    elif computer == 3:
        computer = 's'

    return computer

#Asking player to choose rock , paper or scissors

def user_option():

    ask = input("'rock', 'paper', and 'scissors' " )

    if ask in ['r','rock', 'Rock', 'R']:

        ask = 'r'

    elif ask in [ 'p', 'P' , 'Paper', 'paper']:

        ask = 'p'

    elif ask in ['s' , 'scissors', 'S', 'Scissors']:
        ask = 's'

    else:

        print('Sorry')

    return ask



#THE GAME STARTS HERE!!!!!!


def game_starts():

    player_score = 0

    computer_score = 0

    while player_score != 10 and computer_score != 10:

        player = user_option()

        computer = computer_option()

        if player == 'r':

            if computer == 'p':

                print(f'computer paper attacks player rock')

                computer_score += 1

            elif computer == 's':

                print(f'player rock attacks computer scissors')

                player_score += 1

        elif player == 'p':

            if computer == 's':

                print(f'computer scissors attacks player paper')

                computer_score += 1

            elif computer == 'r':

                print(f'player paper attacks computer rock')

                player_score += 1

        elif player == 's':

            if computer == 'r':

                print(f'computer rock attacks player scissors')

                computer_score += 1

            elif computer == 'p':

                print(f'player scissors attacks computer paper')

                player_score += 1



        print(f'player {player_score}') # Printing out Player's Score

        print(f'computer {computer_score}')# Printing out Computer's Score


        # announcing the winner


        if player_score == 10:` `

            print('player won')

        else:

            print('computer won')




game_starts()


#Asking the player to play the game again

def play_again():

    again = input('play the game again? Yes/No ')

    if again in ['yes', 'Yes' , 'y' , 'Y']:

        again = 'y'

    elif again in ['no', 'No', 'n', 'N']:

        again = 'n'

    return again


replay = play_again()

while replay == 'y':  #Asking user for a replay 

    game_starts()

    if replay == 'n':

        break

1 Ответ

0 голосов
/ 11 января 2020

Если вы посмотрите в этой части кода

replay = play_again()

while replay == 'y':  #Asking user for a replay 

    game_starts()

    if replay == 'n':

        break

Игра начинается снова, а затем заканчивается отображением результатов игрока и компьютера. Но после этого программа переходит к этой строке: -

if replay == 'n':

    break

но в этот момент значение повторного воспроизведения все еще равно 'y', поскольку значение не изменилось после выполнения игры для второго время, так что пока l oop продолжается и продолжается. По сути, вам нужно вызывать функцию play_again () каждый раз после печати результатов игры.

...