在Python中,除了使函数中的所有变量都是全局变量外,还有哪些可能的替代方法?

2024-04-26 09:48:53 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我知道你不应该/真的/让变量成为全局变量-它们应该是局部变量。不过,我正在编程一个21点游戏,每次用户想玩的时候我都需要重置这些卡,所以我做了一个名为“重置”的函数,如果要重复,它会调用这个函数。我的主要问题是,我应该这样做,还是有更好的方法来解决我的问题,还是有更好的方法来做这件事,而不是每次只键入global? 这是我的密码,如果有人想看的话

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

Tags: 方法函数trueglobalstart重置cardsreset
3条回答

只是为了在正确的方向上留下一个缺口:

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

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

使用全局变量是python的一个特性,但是如果您想优雅地编写代码,我认为根本不应该使用全局变量。你知道吗

我喜欢在函数之间传递变量。 我的解决方案可能看起来不太优雅,但在我看来确实如此。你知道吗

注意:我还将3个print语句包含到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

扩展What的答案,您可以删除当前游戏对象并创建新对象,而不是让游戏中的所有卡都静止。然后在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

祝你的比赛一切顺利!你知道吗

相关问题 更多 >