【问题标题】:"Variable" not accessed“变量”未访问
【发布时间】:2023-03-08 13:45:01
【问题描述】:

我正在开发一个二十一点程序,除了我的PlayAgain() 函数外,一切正常。在这个函数中,我应该重置我的变量,但是当我这样做时,变量会变成深蓝色,当悬停在上面时,它会说“变量”未被访问。运行程序并尝试再次播放时,变量仍保留以前的值。我对python很陌生,所以我不知道如何处理这个问题,帮助会很好。

这是我的整个代码:

import random

deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4
dealer = []
hand = []
random.shuffle(deck)

def DealDealer(deck):
    while len(dealer) < 2:
        card = deck.pop()
        if card == 11:card = "J"
        if card == 12:card = "Q"
        if card == 13:card = "K"
        if card == 14:card = "A"
        dealer.append(card)
    return dealer

def DealPlayer(deck):
    while len(hand) < 2:
        card = deck.pop()
        if card == 11:card = "J"
        if card == 12:card = "Q"
        if card == 13:card = "K"
        if card == 14:card = "A"
        hand.append(card)
    return hand

def PlayAgain():
    again = input("\nDo you want to play again? (Y/N) : ").lower()
    if again == "y":
        Game()
        deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4
        dealer = []
        hand = []
        random.shuffle(deck)
    else:
        print("Bye!")
        exit()

def Hit():
    card = deck.pop()
    if card == 11:card = "J"
    if card == 12:card = "Q"
    if card == 13:card = "K"
    if card == 14:card = "A"
    hand.append(card)
    return hand

def DealerHit():
    card = deck.pop()
    if card == 11:card = "J"
    if card == 12:card = "Q"
    if card == 13:card = "K"
    if card == 14:card = "A"
    dealer.append(card)
    return dealer

def HandTotal(hand):
    total = 0
    for card in hand:
        if(card == "J" or card == "Q" or card == "K"):
            total+= 10
        elif(card == "A"):
            if(total >= 11):
                total+= 1
            else: 
                total+= 11
        else:
            total += card
    return total

def DealerTotal(dealer):
    total = 0
    for card in dealer:
        if(card == "J" or card == "Q" or card == "K"):
            total+= 10
        elif(card == "A"):
            if(total >= 11):
                total+= 1
            else: 
                total+= 11
        else:
            total += card
    return total

def DealersRetribution():
    while(DealerTotal(dealer) < HandTotal(hand)):
        DealerHit()
        print("Dealer's Hand: ", dealer)

        if(DealerTotal(dealer) > 21):
            print("\Dealer Busted! You Win!\nYour Score: ", HandTotal(hand), "\nDealer's Score: ", DealerTotal(dealer))
            PlayAgain()
    
def Score():
    if(HandTotal(hand) == DealerTotal(dealer)):
        print("\nTie! I'll getcha next time!\nYour Score: ", HandTotal(hand), "\nDealer's Score: ", DealerTotal(dealer))
    elif(HandTotal(hand) > DealerTotal(dealer)):
        print("What?! You Won?! How?! U Just got lucky.\nYour Score: ", HandTotal(hand), "\nDealer's Score: ", DealerTotal(dealer))
    else:
        print("\nYou Lose!\nYour Score: ", HandTotal(hand), "\nDealer's Score: ", DealerTotal(dealer))

def NormalAction():
    action = input("\nHit - press 1\nStand - press 2\nDouble Down - press 3\n> ")
    if(action == "1"):
        Hit()
        print("\nDealer's Hand: ", dealer)
        print("Your Hand: ", hand)
        if(HandTotal(hand) > 21):
            print("\nYou Busted!\nYour Score: ", HandTotal(hand), "\nDealer's Score: ", DealerTotal(dealer))
            PlayAgain()
        else:
            NormalAction()
    elif(action == "2"):
        print()
        DealersRetribution()
        print("\nDealer's Hand: ", dealer)
        print("Your Hand: ", hand)
        Score()
    elif(action == "3"):
        Hit()
        print("\nDealer's Hand: ", dealer)
        print("Your Hand: ", hand)
        DealersRetribution()
        print("\nDealer's Hand: ", dealer)
        print("Your Hand: ", hand)
        Score()
    else:
        print("\nPlease enter a correct action (1/2)!")
        NormalAction()

def Game():
    play = input("\nWELCOME TO BLACKJACK!\nPlay - press 1\nQuit - press 2\n> ")
    if(play == "2"):
        print("\nBye, Have a nice day!")
        exit()
    elif(play == "1"):
        DealDealer(deck)
        DealPlayer(deck)
        print("\nDealer's Hand: ", dealer[0], "X")
        print("Your Hand: ", hand)
        NormalAction()
        PlayAgain()
    else:
        print("\nPlease enter a correct action (1/2)!")
        Game()

Game()

提前致谢。

ps:我一直在尝试让拆分(二十一点拆分,而不是编程拆分)工作,但不知道如何去做。我原本以为我可以在数组中创建一个数组,但很快意识到这会导致各种问题。对此的帮助也很好,但错误是现在的主要问题

编辑:询问我定义变量的位置。我在程序的最顶端定义了它们。 here

【问题讨论】:

  • 函数中的变量与函数之外的任何东西都没有任何关系,所以你分配给它们的值永远不会用于任何事情。您可能希望创建对象的这些实例属性,并让函数成为可以修改属性的实例方法。
  • 如果你想分配全局变量,你必须在 PlayAgain 中声明它们是全局的,所以在分配它们之前在 PlayAgain 中的global deck, dealer, hand。目前您分配局部变量。当然,另一种方法是将它们作为 Game 的参数,尽可能避免使用全局变量
  • @Samwise 我怀疑这些值从未被使用过,所以我尝试更早地放置一个 return 语句,但这没有用,因为我也在调用一个函数。关于你评论的其余部分,我看到了很多我不完全确定它们的意思的大词。我知道什么是对象、属性和方法,但我不太确定实例是什么。你能给我举个例子吗?

标签: python function blackjack


【解决方案1】:

这是因为 vs-code 将该代码检测为 deadcode 不会在程序中使用。 从下面的例子中会更清楚:

def abc():
 a= input('enter first number')
 b = input('enter second number')
 return a;
alpha  = abc();
print(a)

在上面的例子中,b从未被使用过,因此被vs-code检测为deadcode。所以 vs 代码将这些语句称为不可访问但代码仍会被执行。

在您的情况下,程序中未使用经销商,因此 vs-code 将其视为死代码

【讨论】:

  • 这解决了我的问题。谢谢!
【解决方案2】:

问题是值在 play again 函数中被洗牌,而不是在 Game() 函数中。

你可能应该这样做:

def PlayAgain():
    again = input("\nDo you want to play again? (Y/N) : ").lower()
    if again == "y":
         Game()
    else:
         print("Bye!")
         exit()

并在 Game() 函数的开头实现这部分:

def Game():       
        deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4
        dealer = []
        hand = []
        random.shuffle(deck)
        #rest_of_your_code

如果这不起作用,您还应该分享您的 Game() 函数;)

【讨论】:

  • 这个解决方案只对了一半。事实证明我需要将它们作为全局添加到 Game() 中,否则会给我带来 40 多个错误?
  • 很高兴能帮上忙!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-07
相关资源
最近更新 更多