debugging my .append()函数不能正常工作,我无法找出原因(初级Python)

7qhs6swi  于 2022-11-14  发布在  Python
关注(0)|答案(1)|浏览(148)

我是一个初级程序员,我试图用Python 3.10使用PyCharm CE做一个简单的21点游戏,只是为了练习,但是我不能解决一个问题。问题是我的player_draw_card()函数没有画出一张牌。我想把整个代码放在一起,以防我错过什么,但是我也用#突出了重要的部分(imo)。

import random

cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] #extra 10's stands for J,Q,K and 11 is Ace
player_score = 0
computer_score = 0
player_cards = []
computer_cards = []

#############################################################
def player_draw_card():
  index = random.randint(0, 12)  # Selecting a random index from cards for player
  player_cards.append(cards[index])  # Adding the selected 1 card to player_cards
#############################################################

def computer_draw_card():
  index = random.randint(0, 12)  # Selecting a random index from cards for computer
  computer_cards.append(cards[index])  # Adding the selected 1 card to computer_cards

def calculate_player_score():  # Calculate player's score everytime it draws a card to prevent confusion when you have multiple Aces.
  player_score = sum(player_cards)
  if player_score > 21 and (11 in player_cards):
      index_of_11 = player_cards.index(11)
      player_cards[index_of_11] = 1
      player_score = sum(player_cards)

def calculate_computer_score():  # Calculate computer's score everytime it draws a card to prevent confusion when you have multiple Aces.
  computer_score = sum(computer_cards)
  if computer_score > 21 and (11 in computer_cards):
    index_of_11 = computer_cards.index(11)
    computer_cards[index_of_11] = 1
    computer_score = sum(computer_cards)

#####################################################################
def blackjack():
  player_lost = False
  player_score = 0
  computer_score = 0
  player_cards = []
  computer_cards = []
  player_draw_card() #################
  player_draw_card() #################
###################################################################### 
#**Problem is not beyond here most likely, cause when I run it step by step**
#**player_draw_card() function didnt add anything.**
##########################################################################
  print(f"Your cards: {player_cards}")
  calculate_player_score()
#####################################
  computer_draw_card()
  print(f"Computer's first card: {computer_cards}")
#####################################
#**My computer_draw_card() is also not working btw but they build in same way so**
#**if we can fix the player_draw_card() function, we can fix the computer_draw_card() aswell**
#######################################
  calculate_computer_score()
  player_wants_more = True
  while player_wants_more:
    y_or_n = input("Type 'y' to get another card, type 'n' to pass:").lower()
    if y_or_n == "n":
      player_wants_more = False
    if y_or_n == "y":
      player_draw_card()
      calculate_player_score()
      if player_score > 21:
        player_lost = True
        player_wants_more = False
              
  if player_lost == True:
    print("You lost!\n")
    restart = input("If you want to play again type 'y' if you want to finish the game type 'n'")
    if restart == "y":
      blackjack()
  while computer_score < 17:
    computer_draw_card()
    calculate_computer_score()
  if computer_score < player_score:
    print("You win!\n")
  elif player_score < computer_score:
    print("You lose")
  else:
    print("It's a draw!")
  y_or_n = input("Type 'y' if you want to play another game and 'n' if you dont.").lower()
  if y_or_n == 'y':
    blackjack()
  else:
    print("Bye")
blackjack() ############################### This is the only function that is called 
########################################### Except the ones that are called in def's

我试着在www.example.com中一步一步地查找它pythontutor.com,它看起来像“"“player_cards.append(cards[index])#将选定的1张牌添加到player_cards”"”代码行,没有向player_cards添加任何内容
但奇怪的是,我尝试了一个简单的版本来绘制卡片。

cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
player_cards = []
import random

def player_draw_card():
    index = random.randint(0, 12)  # Selecting a random index from cards for player
    player_cards.append(cards[index])
player_draw_card()
print(player_cards)

它的工作,它增加了一个随机数的球员_卡。有什么问题,有人请帮助我。
先谢了

vjhs03f7

vjhs03f71#

您定义了player_cards列表两次,一次是在第7行定义,另一次是在第41行的blackjack()方法中定义。
当你创建两个作用域不同的变量player_cards时,当你从blackjack()方法调用player_draw_card()时,player_draw_card()并不知道blackjack()方法中的player_cards,它先在自己的作用域中查找player_cards,然后在定义它的父作用域中查找。
您可以通过两种方式解决此问题:

  • 删除blackjack()方法中的player_cards声明。这将使所有方法都使用全局列表。
  • player_draw_card(player_cards: list[int])方法中添加一个参数,并从blackjack()方法中传递列表。在这种情况下,您不再需要全局列表。对于这个代码示例来说,这太复杂了。

注意:compiter_cards列表也有同样的问题。

相关问题