"Variable" 未访问
"Variable" not accessed
我正在开发一个 Blackjack 程序,除了我的 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
问题是在 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() 函数;)
那是因为 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 代码将这些语句引用为 不可访问。但是代码仍然会被执行。
在你的例子中,dealer 没有在程序中使用,所以 vs-code 发现它是 deadcode
我正在开发一个 Blackjack 程序,除了我的 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
问题是在 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() 函数;)
那是因为 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 代码将这些语句引用为 不可访问。但是代码仍然会被执行。
在你的例子中,dealer 没有在程序中使用,所以 vs-code 发现它是 deadcode