简体   繁体   中英

Python Rock - Paper - Scissors Game

My code:

    import random

options = ['Rock', 'Paper', 'Scissors']
gamecontrol = True

player_turn = ' '
computer_turn = ' '


def random_choose(options, player_turn, computer_turn):

    player_turn = input('Enter your decision -> Rock,Paper,Scissors')
    computer_turn = random.choice(options)


def win_check(player_turn, computer_turn):
    if (player_turn == 'Rock' and computer_turn == 'Rock') or (player_turn == 'Paper' and computer_turn == 'Paper') \
            or (player_turn == 'Scissors' and computer_turn == 'Scissors'):
        print('DRAW!')
    elif (player_turn == 'Rock' and computer_turn == 'Scissors') or (player_turn == 'Scissors' and computer_turn ==
                                                                     'Paper') or (
            player_turn == 'Paper' and computer_turn == 'Rock'):
        print('PLAYER WON!')
    elif (player_turn == 'Scissors' and computer_turn == 'Rock') or (player_turn == 'Paper' and computer_turn ==
                                                                     'Scissors') or (
            player_turn == 'Rock' and computer_turn == 'Paper'):
        print('COMPUTER WON!')


while gamecontrol:

    print('WELCOME TO THE ROCK & PAPER & SCISSORS GAME!')
    random_choose(options, player_turn, computer_turn)
    print(f"Player's choice: {player_turn}\nComputer's turn: {computer_turn}")
    win_check(player_turn, computer_turn)

    control = input('Do you want to play again?')
    if control == 'y':
        gamecontrol = True
    else:
        gamecontrol = False

I am trying to write a simple Rock,Paper,Scissors game but when I tried to start the game I am getting results like this:

    WELCOME TO THE ROCK & PAPER & SCISSORS GAME!
Enter your decision -> Rock,Paper,ScissorsPaper
Player's choice:  
Computer's turn:  
Do you want to play again?

Here I can not see the decisions and who won. Can you help me on this please?

You have a scope problem.

You define local variables in your functions that are named the same as those in outer scope - but they are not the same .

k = 22

def func():
    k = 33   # innner scope variable
    print(id(k), k)

print(id(k),k) # global scope variable
func()
print(id(k),k) # global scope variable

Ouput:

140235284185312 22    # different id's == different variables
140235284185664 33
140235284185312 22    # unmodified outer scope variable

Instead of using global variables, return values from your functions. I optimized the "win" check as well:

import random

options = ['Rock', 'Paper', 'Scissors']
gamecontrol = True

def random_choose(options): 
    player_turn = input('Enter your decision -> Rock,Paper,Scissors')
    return player_turn, random.choice(options)

def win_check(player_turn, computer_turn):
    if player_turn == computer_turn:
        print('DRAW!')
    # it is better to compare against tuples here
    elif (player_turn, computer_turn) in { ('Rock', 'Scissors'), 
                                           ('Scissors','Paper'), 
                                           ('Paper', 'Rock') }:
        print('PLAYER WON!')
    # not a draw, not player won - only computer won remains
    else:
        print('COMPUTER WON!')


while gamecontrol: 
    print('WELCOME TO THE ROCK & PAPER & SCISSORS GAME!')
    player_turn ,computer_turn = random_choose(options)

    print(f"Player's choice: {player_turn}\nComputer's turn: {computer_turn}")
    win_check(player_turn, computer_turn)

    control = input('Do you want to play again?')
    if control == 'y':
        gamecontrol = True
    else:
        gamecontrol = False

Output:

WELCOME TO THE ROCK & PAPER & SCISSORS GAME!
Enter your decision -> Rock,Paper,ScissorsRock
Player's choice: Rock
Computer's turn: Paper
COMPUTER WON!
Do you want to play again?N

HTH

You are shadowing the global variables with the functions variables. Either delete them and use the global keyword

def random_choose(options):
    global player_turn
    global computer_turn
    player_turn = input('Enter your decision -> Rock,Paper,Scissors')
    computer_turn = random.choice(options)


def win_check():
    global player_turn
    global computer_turn
    if (player_turn == 'Rock' and computer_turn == 'Rock') or (player_turn == 'Paper' and computer_turn == 'Paper') or (player_turn == 'Scissors' and computer_turn == 'Scissors'):
        print('DRAW!')
    elif (player_turn == 'Rock' and computer_turn == 'Scissors') or (player_turn == 'Scissors' and computer_turn == 'Paper') or (player_turn == 'Paper' and computer_turn == 'Rock'):
        print('PLAYER WON!')
    elif (player_turn == 'Scissors' and computer_turn == 'Rock') or (player_turn == 'Paper' and computer_turn == 'Scissors') or (player_turn == 'Rock' and computer_turn == 'Paper'):
        print('COMPUTER WON!')


while gamecontrol:
    print('WELCOME TO THE ROCK & PAPER & SCISSORS GAME!')
    random_choose(options)
    print(f"Player's choice: {player_turn}\nComputer's turn: {computer_turn}")
    win_check()

    control = input('Do you want to play again?')
    if control == 'y':
        gamecontrol = True
    else:
        gamecontrol = False

or delete them altogether and return the values from random_choose

import random

options = ['Rock', 'Paper', 'Scissors']
gamecontrol = True


def random_choose(options):
    player_turn = input('Enter your decision -> Rock,Paper,Scissors')
    computer_turn = random.choice(options)
    return player_turn, computer_turn


def win_check(player_turn, computer_turn):
    if (player_turn == 'Rock' and computer_turn == 'Rock') or (player_turn == 'Paper' and computer_turn == 'Paper') or (player_turn == 'Scissors' and computer_turn == 'Scissors'):
        print('DRAW!')
    elif (player_turn == 'Rock' and computer_turn == 'Scissors') or (player_turn == 'Scissors' and computer_turn == 'Paper') or (player_turn == 'Paper' and computer_turn == 'Rock'):
        print('PLAYER WON!')
    elif (player_turn == 'Scissors' and computer_turn == 'Rock') or (player_turn == 'Paper' and computer_turn == 'Scissors') or (player_turn == 'Rock' and computer_turn == 'Paper'):
        print('COMPUTER WON!')


while gamecontrol:
    print('WELCOME TO THE ROCK & PAPER & SCISSORS GAME!')
    choices = random_choose(options)
    print(f"Player's choice: {choices[0]}\nComputer's turn: {choices[1]}")
    win_check(choices[0], choices[1])

    control = input('Do you want to play again?')
    if control == 'y':
        gamecontrol = True
    else:
        gamecontrol = False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM