简体   繁体   English

“变量”未访问

[英]"Variable" not accessed

I'm working on a Blackjack program and everything works fine except my PlayAgain() function.我正在开发二十一点程序,除了我的PlayAgain() function 之外,一切正常。 In this function, im supposed to reset my variables but when i do so, the variables are discolored in a darker blue and when hovered over, it says "variable" is not accessed.在这个 function 中,我应该重置我的变量,但是当我这样做时,变量会变成深蓝色,当悬停在上面时,它会说“变量”未被访问。 When running the program and trying to play again, the variables are still left with the previous values.运行程序并尝试再次播放时,变量仍保留以前的值。 I'm pretty new to python so I have no idea how to deal with this, help would be nice.我对 python 很陌生,所以我不知道如何处理这个问题,帮助会很好。

这是问题

here is my ENTIRE code:这是我的整个代码:

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()

Thx in advance.提前谢谢。

ps: I've been trying to get splitting(blackjack splitting, not programming splitting) to work but have no idea how to go about it. ps:我一直在尝试让拆分(二十一点拆分,而不是编程拆分)工作,但不知道如何 go 关于它。 I originally thought that i could just make an array inside an array but quickly realized that caused all sorts of problems.我原本以为我可以在数组中创建一个数组,但很快意识到这会导致各种问题。 Help on this would also be nice but the Error is the main issue right now对此的帮助也很好,但错误是现在的主要问题

edit: a request for where i defined my variables were ask.编辑:询问我在哪里定义我的变量的请求。 I defined them at the very top of my program.我在程序的最顶端定义了它们。 here这里

The problem is that the value is shuffled in the play again function, but not in the Game() function.问题是该值在播放中再次洗牌 function,但在 Game() function 中没有。

What you probably should do is this:你可能应该做的是:

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

And implement this part in the beginning of your Game() function:并在 Game() function 的开头实现这部分:

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

If this does not work you should also share your Game() function;)如果这不起作用,您还应该分享您的 Game() function;)

add another = and it will be a variable添加另一个 = 它将是一个变量

That is because vs-code detect that code as deadcode which is not going to be used in the program.那是因为 vs-code 将该代码检测为不会在程序中使用的代码。 It will get more clear from the example below:从下面的例子中会更清楚:

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

In the example above, b is never used, so it is detected as deadcode by vs-code.在上面的示例中,b 从未使用过,因此被 vs-code 检测为码。 So vs code refer these statements as not accessible .所以 vs 代码将这些语句称为不可访问 But the code will still get executed.但是代码仍然会被执行。

In your case, dealer is not used in the program so vs-code finds it as deadcode在您的情况下,程序中未使用经销商,因此 vs-code 将其视为死代码

Im working on a Blackjack program and everything works fine except my PlayAgain() function.我正在开发二十一点程序,除了我的 PlayAgain() function 外,一切正常。 In this function, im supposed to reset my variables but when i do so, the variables are discolored in a darker blue and when hovered over, it says "variable" is not accessed.在这个 function 中,我应该重置我的变量,但是当我这样做时,变量会变成深蓝色,当悬停在上面时,它会说“变量”未被访问。 When running the program and trying to play again, the variables are still left with the previous values.运行程序并尝试再次播放时,变量仍保留以前的值。 Im pretty new to python so I have no idea how to deal with this, help would be nice.我对 python 很陌生,所以我不知道如何处理这个问题,帮助会很好。

这是问题

here is my ENTIRE code:这是我的整个代码:

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()

Thx in advance.提前谢谢。

ps: Ive been trying to get splitting(blackjack splitting, not programming splitting) to work but have no idea how to go about it. ps:我一直在尝试让拆分(二十一点拆分,而不是编程拆分)工作,但不知道如何 go 关于它。 I originally thought that i could just make an array inside an array but quickly realized that caused all sorts of problems.我原本以为我可以在数组中创建一个数组,但很快意识到这会导致各种问题。 Help on this would also be nice but the Error is the main issue right now对此的帮助也很好,但错误是现在的主要问题

edit: a request for where i defined my variables were ask.编辑:询问我在哪里定义我的变量的请求。 I defined them at the very top of my program.我在程序的最顶端定义了它们。 here这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM