首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >巨蟒石头-剪刀游戏

巨蟒石头-剪刀游戏
EN

Stack Overflow用户
提问于 2019-12-22 12:36:05
回答 2查看 217关注 0票数 0

我的代码:

代码语言:javascript
复制
    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

我试图写一个简单的岩石,纸,剪刀游戏,但当我试图开始游戏时,我得到了这样的结果:

代码语言:javascript
复制
    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?

在这里我看不出决定和谁赢了。能帮我一下吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-22 12:46:40

你的范围有问题。

在函数中定义与外部作用域中相同的局部变量,但它们并不相同。

代码语言:javascript
复制
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

输出:

代码语言:javascript
复制
140235284185312 22    # different id's == different variables
140235284185664 33
140235284185312 22    # unmodified outer scope variable

不要使用全局变量,而是从函数中返回值。我还优化了“胜利”检查:

代码语言:javascript
复制
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

输出:

代码语言:javascript
复制
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

票数 2
EN

Stack Overflow用户

发布于 2019-12-22 12:47:09

您正在用函数变量跟踪全局变量。要么删除它们,然后使用global关键字

代码语言:javascript
复制
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返回值。

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59444252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档