二十一点游戏不工作
Blackjack game doesnt work
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 有点陌生,我试图创建一个简单的二十一点游戏。它并不完全按预期工作。当我站起来时,我得到一个永无止境的循环 "do you want to hit or stand? [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
在代码停止之前,我不明白为什么会这样。它给出了一个循环,你想打还是站,直到它突然停止并突然添加另一张牌而我没有打
问题是 "drawPlayerCard()" 调用 "drawComputerHand()" 而 "drawComputerHand()" 调用 "hitStand()"。
解释:
"drawPlayerHand()"中的第一个问题:
if len(playerHand) < 2:
drawPlayerCard()
drawComputerHand()
当您的 if 语句为真时,"drawPlayerHand()" 将执行第二次。也就是说"drawComputerHand()"也执行了2次
你在 "drawComputerHand()" 中遇到了同样的问题:
if len(computerHand) < 2:
drawComputerHand()
hitStand()
尝试自己修复。如果您无法修复它,我可以 post 更多代码。
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 有点陌生,我试图创建一个简单的二十一点游戏。它并不完全按预期工作。当我站起来时,我得到一个永无止境的循环 "do you want to hit or stand? [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
在代码停止之前,我不明白为什么会这样。它给出了一个循环,你想打还是站,直到它突然停止并突然添加另一张牌而我没有打
问题是 "drawPlayerCard()" 调用 "drawComputerHand()" 而 "drawComputerHand()" 调用 "hitStand()"。
解释:
"drawPlayerHand()"中的第一个问题:
if len(playerHand) < 2:
drawPlayerCard()
drawComputerHand()
当您的 if 语句为真时,"drawPlayerHand()" 将执行第二次。也就是说"drawComputerHand()"也执行了2次
你在 "drawComputerHand()" 中遇到了同样的问题:
if len(computerHand) < 2:
drawComputerHand()
hitStand()
尝试自己修复。如果您无法修复它,我可以 post 更多代码。