python 21纸牌游戏问题

时间:2019-02-12 20:12:30

标签: 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()

2 个答案:

答案 0 :(得分:1)

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

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

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

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

成为:

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

ace2函数中的ace3draw类似。在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()来接受字符串作为输入。

  

这也发生在经销商身上,他陷入了无尽的困境   绘制卡片的循环,删除前一张卡片,然后添加   卡。

我编辑了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获胜,或者他破产了。您有责任改善代码并让经销商留下来。

答案 1 :(得分:0)

哇.. em花了一段时间,但我想我终于让它工作了:)

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---")