回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我是一个Python新手,但我试过写21点脚本。在调试并纠正了所有明显的错误之后,我遇到了一个我无法理解的奇怪事件。你知道吗</p>
<p>当<code>total</code>是>;21时,它似乎跳过了<code>while (total < 21) and (stand != True):</code>代码块,即使在游戏循环的开始我将所有相关变量归零。你知道吗</p>
<p>我花了太多的时间试图弄清楚这一点,我不能不认为这有一个明显的解决办法。你知道吗</p>
<p>我不明白为什么<code>while (total < 21) and (stand != True):</code>似乎被跳过,即使它应该是一个真正的声明在每一场比赛的开始。你知道吗</p>
<p>下面是完整的代码。你可以自己测试一下,看看我的意思。你知道吗</p>
<pre><code>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!")
</code></pre>