Angela Yu 博士的 100 天编程中的 Blackjack Capstone 项目

问题描述 投票:0回答:3

这个问题与Angela Yu博士的Python教程第11天有关。我无法执行我输入的代码。该代码是在 replit 中输入的。我哪里出错了?这段代码应该用来玩二十一点游戏。

import random
from replit import clear
from art import logo

def draw_card():
  cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
  return random.choice(cards)

def calculate_score(cards):
  if sum(cards) == 21 and len(cards) == 2:
    return 0
  if sum(cards) > 21 and 11 in cards:
    cards.remove(11)
    cards.append(1)
  return sum(cards)

def compare(user_score, computer_score):
  if user_score > 21 and computer_score > 21:
    print("You went over 21. You lost")
  elif computer_score == 0:
    print("You lost. Computer has blackjack")
  elif user_score == 0:
    print("You won with a blackjack.")
  elif user_score == computer_score:
    print("Draw")
  elif user_score > 21:
    print("You lost")
  elif computer_score > 21:
    print("you won")
  elif user_score > computer_score:
    print("You won.")
  else:
    print("Computer won")

def play_game():
  print (logo)
  user_cards = []
  computer_cards = []
  for number in range(2):
    user_cards.append(draw_card())
    computer_cards.append(draw_card())
  
  game_end = False
  
  while not game_end:
  
    user_score = calculate_score(user_cards)
    computer_score = calculate_score(computer_cards)
    
    print(f"   Your cards: {user_cards}, your score: {user_score}.")
    print(f"   Computer's first card: {computer_cards[0]}")
    
    get_card = input("Type 'y' to get another card, type 'n' to pass: ")
    if get_card == "y".lower():
      user_cards.append(draw_card())
  
    else:
      game_end = True
      while computer_score < 17:
        computer_cards.append(draw_card)
  
  print(f"   Your final hand: {user_cards}, final score: {user_score}")
  print(f"   Computer's final hand: {computer_cards}, final score: {computer_score}")
  print(compare(user_score, computer_score))


play = input("Do you want to play a game of blackjack. Type y or n: ").lower()
while play == "y":
  clear()
  play_game()
  




我无法在 thonny 中调试此代码,因为某些功能只能在 replit 中找到。

python function while-loop project blackjack
3个回答
1
投票

您永远不会重新计算

computer_score
,因此
computer_score < 17
将永远保留
True


0
投票
"while computer_score < 17:
        computer_cards.append(draw_card)"

shift it backward by one it should be in the same line as while not game_end:


import random
from replit import clear
from art import logo

def draw_card():
  cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
  return random.choice(cards)

def calculate_score(cards):
  if sum(cards) == 21 and len(cards) == 2:
    return 0
  if sum(cards) > 21 and 11 in cards:
    cards.remove(11)
    cards.append(1)
  return sum(cards)

def compare(user_score, computer_score):
  if user_score > 21 and computer_score > 21:
    print("You went over 21. You lost")
  elif computer_score == 0:
    print("You lost. Computer has blackjack")
  elif user_score == 0:
    print("You won with a blackjack.")
  elif user_score == computer_score:
    print("Draw")
  elif user_score > 21:
    print("You lost")
  elif computer_score > 21:
    print("you won")
  elif user_score > computer_score:
    print("You won.")
  else:
    print("Computer won")

def play_game():
  print (logo)
  user_cards = []
  computer_cards = []
  for number in range(2):
    user_cards.append(draw_card())
    computer_cards.append(draw_card())
  
  game_end = False
  
  while not game_end:
  
    user_score = calculate_score(user_cards)
    computer_score = calculate_score(computer_cards)
    
    print(f"   Your cards: {user_cards}, your score: {user_score}.")
    print(f"   Computer's first card: {computer_cards[0]}")
    
    get_card = input("Type 'y' to get another card, type 'n' to pass: ")
    if get_card == "y".lower():
      user_cards.append(draw_card())
  
    else:
      game_end = True
  while computer_score < 17:
     computer_cards.append(draw_card)
  
  print(f"   Your final hand: {user_cards}, final score: {user_score}")
  print(f"   Computer's final hand: {computer_cards}, final score: {computer_score}")
  print(compare(user_score, computer_score))


  play = input("Do you want to play a game of blackjack. Type y or n: ").lower()
  while play == "y":
     clear()
     play_game()
  

-2
投票

您的代码中存在错误。以下是解决方案。 这样做:


#The main bug is that the program gets stuck at while loop in around lineNO 62 where it says "while computer_score < 17:"
#I could solve it for you but i don't know the game, so do something there.
#My suggestion: use if statement instead of while loop.
© www.soinside.com 2019 - 2024. All rights reserved.