在Python中使函数全局变量的可能替代方法是什么?

时间:2018-04-27 21:10:48

标签: python

好的,所以我知道你不应该/真的/使变量变得全局 - 它们本来就是本地的。然而,我正在编写一个二十一点游戏,我需要在每次用户想要再次播放时重置卡片 - 所以我创建了一个名为“重置”的功能,并且如果要重复它则调用它。我的主要问题是,我应该这样做,还是有更好的方法来解决我的问题,还是有更好的方法来做到这一点,而不是每次都输入全球_____? 如果有人想看,这是我的代码;

import random
playagain = True
def reset():
    global cards
    global gameCards
    global playerCards
    global cpuCards
    global pgameCards
    global cpugameCards
    global start
    cards = ["A♠", "2♠", "3♠", "4♠", "5♠", "6♠", "7♠", "8♠", "9♠", "10♠", "J♠", "Q♠", "K♠",
             "A♥", "2♥", "3♥", "4♥", "5♥", "6♥", "7♥", "8♥", "9♥", "10♥", "J♥", "Q♥", "K♥",
             "A♣", "2♣", "3♣", "4♣", "5♣", "6♣", "7♣", "8♣", "9♣", "10♣", "J♣", "Q♣", "K♣",
             "A♦", "2♦", "3♦", "4♦", "5♦", "6♦", "7♦", "8♦", "9♦", "10♦", "J♦", "Q♦", "K♦"]
    gameCards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
                 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
                 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
                 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
    playerCards = []
    cpuCards = []
    pgameCards = []
    cpugameCards = []
    start = 0

[...]

while playagain == True:
    reset()
    winner = game()
    print("Your cards: ", playerCards)
    print("CPU's cards: ", cpuCards)
    print(winner)
    askUser = input("Play again? Y/N ")
    if askUser.upper() == "Y":
        playagain = True
    else:
        playagain = False

3 个答案:

答案 0 :(得分:0)

只是为了向正确的方向迈进:

import random

playagain = True

class Game:
    def __init__(self):
        # init all members
        pass

    def Reset(self):
        self.cards = ["A?", "2?", "3?", "4?", "5?", "6?", "7?", "8?", "9?", "10?", "J?", "Q?", "K?",
                 "A?", "2?", "3?", "4?", "5?", "6?", "7?", "8?", "9?", "10?", "J?", "Q?", "K?",
                 "A?", "2?", "3?", "4?", "5?", "6?", "7?", "8?", "9?", "10?", "J?", "Q?", "K?",
                 "A?", "2?", "3?", "4?", "5?", "6?", "7?", "8?", "9?", "10?", "J?", "Q?", "K?"]
        self.gameCards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
                     1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
                     1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
                     1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
        self.playerCards = []
        self.cpuCards = []
        self.pgameCards = []
        self.cpugameCards = []
        self.start = 0

这里有一个Object,它包含对象可以执行的所有方法。你必须阅读一些关于面向对象编程的知识,并了解它在python中的实现方式。

答案 1 :(得分:0)

使用全局变量是python的一个特性,但如果你想优雅地编码,我相信全局变量根本不应该被使用。

我喜欢将变量从函数传递给函数。 我的解决方案看起来可能不是很优雅,但实际上我认为是这样。

注意: 我还将3个打印语句包含在game()函数中,以使其更简单,因为它们被调用,无论是什么游戏输出是。

这是我的解决方案:

import random
playagain = True
def reset():
    cards = ["A♠", "2♠", "3♠", "4♠", "5♠", "6♠", "7♠", "8♠", "9♠", "10♠", "J♠", "Q♠", "K♠",
             "A♥", "2♥", "3♥", "4♥", "5♥", "6♥", "7♥", "8♥", "9♥", "10♥", "J♥", "Q♥", "K♥",
             "A♣", "2♣", "3♣", "4♣", "5♣", "6♣", "7♣", "8♣", "9♣", "10♣", "J♣", "Q♣", "K♣",
             "A♦", "2♦", "3♦", "4♦", "5♦", "6♦", "7♦", "8♦", "9♦", "10♦", "J♦", "Q♦", "K♦"]
    gameCards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
                 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
                 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
                 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
    playerCards = []
    cpuCards = []
    pgameCards = []
    cpugameCards = []
    start = 0
    return cards, gameCards, playerCards, cpuCards, pgameCards, cpugameCards, start



def game(cards, gameCards, playerCards, cpuCards, pgameCards, cpugameCards, start):

    # [Main Code]

    winner = 'abcd' # put whatever you return from your original game() function as the value of winner 
    print("Your cards: ", playerCards)
    print("CPU's cards: ", cpuCards)
    print(winner)

while playagain == True:
    cards, gameCards, playerCards, cpuCards, pgameCards, cpugameCards, start = reset()
    game(cards, gameCards, playerCards, cpuCards, pgameCards, cpugameCards, start)
    askUser = input("Play again? Y/N ")
    if askUser.upper() == "Y":
        playagain = True
    else:
        playagain = False

答案 2 :(得分:0)

延长What的答案,你可以删除当前的Game对象并创建新对象,而不是在游戏中放置所有牌。然后在init函数中设置所有初始状态。以下是一个例子

import random

class Game:
    cards = []
    gameCards = []
    playerCards = []
    pgameCards = []
    cpugameCards = []
    start = 0
    '''+all your class attributes'''

    def __init__(self):
        '''insert your initial states here'''
        pass

然后,为了开始一个新游戏,您将执行以下操作:

'''To start a new game'''
game = Game()

然后,要完成当前游戏,您可以执行以下操作:

'''To end the current game'''
del game

如果您重置游戏不是技术上创建新游戏吗?这就是我建议创建和删除游戏对象的原因

您不想处理全局变量的原因可以在here

找到

你的游戏一切顺利!