【发布时间】:2017-06-16 06:53:49
【问题描述】:
我是 Python 新手,但我尝试过编写二十一点脚本。在调试并纠正了所有明显的错误之后,我遇到了一个我无法弄清楚的奇怪事件。
当total >21 时,它似乎跳过了while (total < 21) and (stand != True): 代码块,即使在游戏循环开始时我将所有相关变量归零。
我花了太多时间试图解决这个问题,我不禁认为这是一个明显的解决方案。
我不明白为什么 while (total < 21) and (stand != True): 似乎被跳过了,即使它应该是每场比赛开始时的真实陈述。
下面是完整的代码。随意测试一下,看看我的意思。
import pygame
import random
print("Welcome to PyBlackjack V1.0")
done = False
while not done:
# --- Reset our Hands ---
dealerhand = 0
split = False
stand = False
total = 0
# --- Dealing the Player's hand. ---
print("Dealer is dealing hand.")
firstcard = random.randrange(1, 15)
print("First card:",str(firstcard),)
secondcard = random.randrange(1, 15)
print("Second card:",str(secondcard),)
total = (firstcard + secondcard)
print("Hand:",str(total),)
# --- Bust Check ---
if total > 21:
print("Bust! Game Over.")
newgame = input("Play again? Y/N: ")
if str(newgame) == "n":
done = True
break
else:
print("Starting new game! Good Luck!")
dealerfirstcard = random.randrange(1, 15)
dealerholecard = random.randrange(1, 15)
dealerhand = (dealerfirstcard + dealerholecard)
print("Dealer's Hand:",str(dealerfirstcard))
# --- Player decides what to do ---
while (total < 21) and (stand != True):
if split != True:
print("Hand:",str(total))
elif split == True:
print("Left hand:",str(lefthand),"| Right hand:",str(righthand))
playerchoice = input("Hit (1), Double Down(2), Split(3), Stand(4)?")
if int(playerchoice) == 1:
total += random.randrange(1, 15)
elif int(playerchoice) == 2:
#Reserved
break
elif int(playerchoice) == 3:
if ((firstcard + secondcard) / 2) == firstcard and split != True:
lefthand = (firstcard + random.randrange(1, 15))
righthand = (secondcard + random.randrange(1, 15))
split = True
else:
print("You cannot split this hand!")
elif int(playerchoice) == 4:
print("You stand.")
stand = True
else:
print("Invalid Choice!")
print("Hand:",total,)
if total > 21:
print("Bust! Game Over.")
newgame = input("Play again? Y/N: ")
if str(newgame) == "n":
done = True
break
else:
print("Starting new game! Good Luck!")
print("Dealer reveals hole card...")
print("Dealer Hand:",str(dealerhand),)
# --- Dealer hits until >= 17 ---
while dealerhand < 17:
print("Dealer hits...")
dealerhand = (dealerhand + random.randrange(1, 15))
print("Dealer hand:",dealerhand,)
# --- Deciding who wins ---
if dealerhand > 21:
print("Dealer busts! You win!")
elif dealerhand >= 17:
print("Your hand:",total,"| Dealer hand:",dealerhand,)
if split != True:
if dealerhand >= total:
print("You lose!")
elif dealerhand < total:
print("You win!")
elif split == True:
if lefthand > dealerhand:
print("Left hand wins!")
elif lefthand < dealerhand:
print("Left hand loses!")
else:
print("An error occured. Ending program.")
done = True
break
if righthand > dealerhand:
print("Right hand wins!")
elif righthand < dealerhand:
print("Right hand loses!")
else:
print("An error occured. Ending program.")
done = True
break
# --- To loop or not to loop ---
newgame = input("Play again? Y/N: ")
if str(newgame) == "n":
done = True
break
else:
print("Starting new game! Good Luck!")
【问题讨论】:
-
在测试之前:“stand != True”应该是“stand is not True”或“stand is False”。不要用 True 或 False 来检查相等性。使用“是真”或“是假”
-
我不明白这个问题。如果 Total > 21,那么它不应该进入条件为 Total 的 while 循环。你最大的问题是组织。您正在尝试将其作为一个纯粹的程序系统来执行(一步一步,将每一步编写为循环、条件、输入、输出)。如果您将二十一点的“部分”分解为函数,然后以程序方式调用这些函数,您将能够更轻松地了解每一步的情况。