Python 21扑克游戏问题

2024-10-02 12:25:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在玩21点21点纸牌游戏,但我似乎无法再抽到第三张牌上。再加一张牌,再加上一张牌,再加上一张牌也会发生。在

抱歉,我还是不知道我在做什么。(用这个或Python)

import random

for deal in range(1,2):
  card1=random.randrange(1,12)
  card2=random.randrange(1,12)
  if card1 == 1 or card1 == 11:
    ace1 = int(input("Would you like a 1 or 11?"))
    if ace1 == 1:
      card1 = 1
    elif ace1 == 11:    
      card1 = 11
    else:
      print "not a choice. P.S. I'm too lazy/don't know how to loop this, so you better restart"
  print card1
  if card2 == 1 or card2 == 11:
    ace2 = int(input("Would you like a 1 or 11?"))
    if ace2 == 1:
      card2 = 1
    elif ace2 == 11:
      card2 = 11
    else:
      print "not a choice. P.S. I'm too lazy/don't know how to loop this, so you better restart"
  print card2
  hand = card1+card2
  if hand == 21:
    print "You win"
    print "You're hand is", hand
  elif hand > 21:
    print "You busted"
    print "You're hand is", hand
  else:
    print "You're hand is", hand
for deal in range(1,2):
  dealer_card1=random.randrange(1,12)
  dealer_card2=random.randrange(1,12)
  dealer_cards=dealer_card1 + dealer_card2
  print "The dealer has drawn his hand"
  if dealer_cards == 21:
    print "Dealer won"
  elif dealer_cards > 21:
    print "Dealer busted, you won"
  else:
    print "You're turn"
dealer_hand = [dealer_card1,dealer_card2]
player_hand = [card1,card2]

def draw():
  draw = input("Would you like to [H]it or [S]tay?")
  if draw == "h":
    card3 = random.randrange(1,12)
    if card3 == 1 or card3 == 11:
      ace3 = int(input("Would you like a 1 or 11?"))
      if ace3 == 1:
        card3 = 1
      elif ace3 == 11:
        card3 = 11
      else:
        print "not a choice. P.S. I'm too lazy/don't know how to loop this, so you better restart"
    print "You drew a", card3
    hand1 = hand + card3
    player_hand = [card1,card2,card3]
    print hand1
    if hand1 == 21:
      print "You won"
    elif hand1 > 21:
      print "You busted"
    elif hand1 > 15:
      print "So close"
      print "Dealer's turn"
      print dealerdraw()
    else:
      print "you've got a way to go"
      print "Dealer's turn"
      print dealerdraw()
  elif draw == "s":
    if hand >= 16:
      print "Nice play"
      print "Dealer's turn"
      print dealerdraw()
    elif hand == 21:
      print "You won"
    else:
      print "Not the best play"
      print "Dealer's turn"
      print dealerdraw()
  else:
    print "not a choice. P.S. I'm too lazy/don't know how to loop this, so you better restart"
  return " "

def dealerdraw():
  if dealer_cards < 21:
    dealer_card3 = random.randrange(1,12)
    dealer_hand = [dealer_card1,dealer_card2,dealer_card3]
    dealer_cards1 = dealer_cards + dealer_card3
    print "Dealer drew a card"
    if dealer_cards1 == 21:
      print "Dealer won"
      print "his hand was", dealer_hand, "totaling", dealer_cards1
    elif dealer_cards1 > 21:
      print "Dealer busted, you won"
      print "his hand was", dealer_hand, "totaling", dealer_cards1
    else:
      print "The Dealer smirks"
      print "You're turn"
      print draw()
  elif dealer_cards == 21:
    print "Dealer won, his hand was", dealer_hand, "totaling", dealer_cards1
  else:
    print "If you see this, the game isn't working"
  return " "
print draw()

Tags: oryouifrandomelseprinthandelif
2条回答

哇。。他们花了一段时间,但我想我终于成功了:)

import random

hand = 0

def draw():
  global hand
  hand = 0
  draw = input("Would you like to [H]it or [S]tay?")

  if draw == "h":

    card3 = random.randrange(1,12)

    if card3 == 1 or card3 == 11:

      ace3 = int(input("Would you like a 1 or 11?"))

      if ace3 == 1:

        card3 = 1

      elif ace3 == 11:

        card3 = 11

      else:

        print ("not a choice. P.S. I'm too lazy/don't know how to loop this, so you better restart")

    print ("You drew a", card3)

    hand1 = hand + card3

    player_hand = [card1,card2,card3]

    print (hand1)
    if hand1 == 21:

      print ("You won")


    elif hand1 > 21:

      print ("You busted")


    elif hand1 > 15:

      print ("So close")

      print ("Dealer's turn")

      print (dealerdraw())

    else:

      print ("you've got a way to go")

      print ("Dealer's turn")

      print (dealerdraw())

  elif draw == "s":

    if hand >= 16:

      print ("Nice play")

      print ("Dealer's turn")

      print (dealerdraw())

    elif hand == 21:

      print ("You won")

    else:

      print ("Not the best play")

      print ("Dealer's turn")

      print (dealerdraw())

  else:

    print ("not a choice. P.S. I'm too lazy/don't know how to loop this, so you better restart")



def dealerdraw():
  global dealer_cards
  global hand
  if dealer_cards < 21:

    dealer_card3 = random.randrange(1,12)

    dealer_cards += dealer_card3

    print (dealer_cards)
    dealer_cards1 = dealer_cards + dealer_card3

    print ("Dealer drew a card of", dealer_cards)

    if dealer_cards == 21:

      print ("Dealer won")

      print ("his hand was", dealer_hand, "totaling", dealer_cards1)


    elif dealer_cards > 21:

      print ("Dealer busted, you won")

      print ("his hand was", dealer_hand, "totaling", dealer_cards1)


    else:
      if hand > dealer_cards:
        print("you won")
      else: 
        print ("The Dealer won")

  elif dealer_cards == 21:

    print ("Dealer won, his hand was", dealer_hand, "totaling", dealer_cards1)

  else:

    print ("If you see this, the game isn't working")




while True:
    for deal in range(1,2):

      card1=random.randrange(1,12)

      card2=random.randrange(1,12)

      if card1 == 1 or card1 == 11:

        ace1 = int(input("Would you like a 1 or 11?"))

        if ace1 == 1:

          card1 = 1

        elif ace1 == 11:

          card1 = 11

        else:

          print ("not a choice. P.S. I'm too lazy/don't know how to loop this, so you better restart")

      print (card1)

      if card2 == 1 or card2 == 11:

        ace2 = int(input("Would you like a 1 or 11?"))

        if ace2 == 1:

          card2 = 1

        elif ace2 == 11:

          card2 = 11

        else:

          print ("not a choice. P.S. I'm too lazy/don't know how to loop this, so you better restart")

      print (card2)

      hand = card1+card2

      if hand > 21:

        print ("You busted")

        print ("You're hand is", hand)


      else:

        print ("You're hand is", hand)


    for deal in range(1,2):

      dealer_card1=random.randrange(1,12)

      dealer_card2=random.randrange(1,12)

      dealer_cards=dealer_card1 + dealer_card2

      print ("The dealer has drawn his hand of", dealer_cards)

      if dealer_cards == 21:

        print ("Dealer won")

      elif dealer_cards > 21:

        print ("Dealer busted, you won")


      else:

        print ("You're turn")

    dealer_hand = [dealer_card1,dealer_card2]

    player_hand = [card1,card2]



    draw()
    print(" -NEW GAME -")

在提供的代码中,print语句表明您正在使用python2,但是各种input()调用似乎是python3。在

我认为您使用的是python2,否则编译器将抱怨并且不会打印任何内容。在

在这种情况下,以pyhton2样式重写输入:

ace1 = int(input("Would you like a 1 or 11?"))

变成:

^{pr2}$

类似于ace2和{}函数中的draw。在python2中,input()已经计算了输入,因此如果输入了一个整数,就已经得到了一个整数。在

draw = input("Would you like to [H]it or [S]tay?")

变成:

draw = raw_input("Would you like to [H]it or [S]tay?")

这可能就是问题所在。input()试着把字符串当作一个变量来处理,当然这是不存在的。在python2中,您需要raw_input()来接受一个字符串作为输入。在

This also happens with the dealer, who just gets stuck in an endless loop of drawing a card, removing the previous card, than adding that card.

我编辑了dealerdraw()来解决这一部分:

def dealerdraw():
    global dealer_cards
    if dealer_cards < 21:
        dealer_card3 = random.randrange(1,12)
        dealer_hand.append(dealer_card3) #add the card to the dealer's list cards
        dealer_cards = sum(dealer_hand) #get the sum of all the cards
        print dealer_hand #just to show that cards in dealer hands are added, remove this line in real game
        print "Dealer drew a card"
        if dealer_cards == 21:
            print "Dealer won"
            print "his hand was", dealer_hand, "totaling", dealer_cards
        elif dealer_cards > 21:
            print "Dealer busted, you won"
            print "his hand was", dealer_hand, "totaling", dealer_cards
        else:
            print "The Dealer smirks"
            print "You're turn"
            print draw()
    elif dealer_cards == 21:
        print "Dealer won, his hand was", dealer_hand, "totaling", dealer_cards
    else:
        print "If you see this, the game isn't working"
    return " "

庄家总是抽一张牌。但现在他手上的牌都加好了,或者他赢了21张,否则他就破产了。由你来改进代码,让经销商留下来。在

相关问题 更多 >

    热门问题