Python Rock-纸-剪刀游戏

时间:2019-12-22 12:36:05

标签: python

我的代码:

    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

我试图编写一个简单的Rock,Paper,Scissors游戏,但是当我尝试启动该游戏时,却得到如下结果:

    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?

在这里我看不到决定和谁赢了。你能帮我吗?

2 个答案:

答案 0 :(得分:2)

您遇到示波器问题。

您在函数中定义的局部变量的名称与外部作用域中的变量相同-但它们不相同

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

输出:

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

从函数中返回值,而不是使用全局变量。我还优化了“赢”支票:

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

输出:

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

答案 1 :(得分:2)

您正在用函数变量遮盖全局变量。删除它们并使用global关键字

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

或完全删除它们,然后从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