首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Blackjack Capstone项目从100天代码开始与Angela Yu博士合作

Blackjack Capstone项目从100天代码开始与Angela Yu博士合作
EN

Stack Overflow用户
提问于 2022-11-20 08:46:00
回答 2查看 40关注 0票数 1

这个问题与余安琪博士第11天的Python教程有关。我无法执行我输入的代码。代码是在复制中输入的。我在哪里犯错误?这段代码是用来玩Blackjack游戏的。

代码语言:javascript
复制
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()

由于一些只能在应答中找到的函数,我无法在其中调试这段代码。

EN

回答 2

Stack Overflow用户

发布于 2022-11-20 17:32:15

您永远不会重新计算computer_score,因此computer_score < 17将永远保持True

票数 0
EN

Stack Overflow用户

发布于 2022-11-20 09:32:42

您的code.Here中有bug是解决方案。这样做:

代码语言:javascript
复制
#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.
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74506716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档