while循环不适用于二十一点

时间:2017-08-10 20:02:46

标签: python function while-loop blackjack

我基本上是使用python创建一个简单的二十一点游戏。我试图通过询问玩家是否想要抽牌或不抽牌来做一个循环。但是,它不起作用,有人可以帮忙搞清楚吗?谢谢!

错误是while循环没有生成draw_cards函数,如果我把' Y'或者打印消息,如果我放了' N'

,就会打破循环
import random
def draw_cards(): #this is a function to draw a card from the deck
  ranks =(2,3,4,5,6,7,8,9,10,'J','Q','K','A')
  suits =('Club','Dice','Heart','Spade')
  card = random.choice(suits) + ' ' +str(random.choice(ranks))
  return [card]


def stay_hit(): #this is to decide if the player wants to draw a card
  while True:
    playerinput=input('would you like to draw one more card? Y or 
    N').upper
    if playerinput == 'Y':
        return draw_cards()
        continue
    elif playerinput == 'N':
        print('Ok, please show your cards')
        break

1 个答案:

答案 0 :(得分:2)

您的代码可以进行一些小修改:

import random
def draw_cards(): #this is a function to draw a card from the deck
  ranks =(2,3,4,5,6,7,8,9,10,'J','Q','K','A')
  suits =('Club','Dice','Heart','Spade')
  card = random.choice(suits) + ' ' +str(random.choice(ranks))
  return [card]


def stay_hit(): #this is to decide if the player wants to draw a card
  while True:
    playerinput=input('would you like to draw one more card? Y or N').upper()
    if playerinput == 'Y':
        print(draw_cards())
        continue
    elif playerinput == 'N':
        print('Ok, please show your cards')
        break

stay_hit()