【发布时间】:2018-04-30 15:57:06
【问题描述】:
import random
deckOfCards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
playerHand = []
computerHand = []
def testWin():
if sum(playerHand) == sum(computerHand):
print("Draw")
elif sum(playerHand) == 21:
print("Blackjack! You win")
elif sum(computerHand) == 21:
print("Computer has blackjack you lose")
if sum(playerHand) > 21:
if sum(computerHand) < 21:
print("You lost")
elif sum(computerHand) > 21:
print("Draw")
elif sum(computerHand) > 21:
if sum(playerHand) < 21:
print("You win")
elif sum(playerHand) > 21:
print("Draw")
elif sum(playerHand) < 21:
if sum(computerHand) > 21:
print("You win!")
elif sum(computerHand) < 21 and sum(computerHand) < sum(playerHand):
print("You win")
elif sum(computerHand) < 21 and sum(computerHand) > sum(computerHand):
print("You lose")
def drawPlayerCard():
playerHand.append(deckOfCards[random.randint(0, 9)])
print("Your Cards are:", playerHand)
print("total:", sum(playerHand), "\n")
if len(playerHand) < 2:
drawPlayerCard()
drawComputerHand()
def drawComputerHand():
if sum(computerHand) <= 17:
computerHand.append(deckOfCards[random.randint(0, 9)])
print("the computer has:", computerHand)
print("total:", sum(computerHand), "\n")
if len(computerHand) < 2:
drawComputerHand()
hitStand()
else:
print("the computer stands with a total of:", sum(computerHand))
hitStand()
def hitStand():
option = input("do you want to hit or stand? [h/s]")
if option.lower() == "h":
drawPlayerCard()
elif option.lower() == "s":
testWin()
else:
print("please say if you want to hit or stand!")
hitStand()
def start():
startGaming = input("Do you want to play Blackjack? [y/n]")
if startGaming == "y":
drawPlayerCard()
elif startGaming == "n":
pass
else:
print("please state if you want to start the game")
start()
start()
嘿,我对 pyhton 有点陌生,我试图创建一个简单的二十一点游戏。它并不完全按预期工作。当我站起来时,我会得到一个永无止境的循环“你想打还是站?[h/s]”或类似的事情
do you want to hit or stand? [h/s]h
Your Cards are: [10, 2, 4]
total: 16
the computer has: [9, 6, 4]
total: 19
do you want to hit or stand? [h/s]s
Your Cards are: [10, 2, 4, 3]
total: 19
Draw
do you want to hit or stand? [h/s]s
Your Cards are: [10, 2, 4, 3, 4]
total: 23
You lost
the computer stands with a total of: 19
do you want to hit or stand? [h/s]s
Your Cards are: [10, 2, 4, 3, 4, 2]
total: 25
You lost
在代码停止之前,我无法弄清楚它为什么会这样。它给出了一个你想打还是站的循环,直到它突然停止并突然添加另一张牌而没有我打
【问题讨论】:
标签: python-3.x blackjack