<p>下面是(某种)工作代码。它仍然实现了21点的一个奇数变体(等级从1到15,没有1或11的王牌,击球后可以分开)。一般来说,分裂在这里处理得不好。。。我想你可以分开,然后仍然打/等等。?但是击打不会更新任何一只分开的手,再次分开也不会起任何作用。我把那些逻辑错误留给你去解决。你知道吗</p>
<p>我认为你所描述的问题最好用@Martin的回答来解释。最后我用一个<code>else</code>来简化这个逻辑,以处理非半身像的情况。顺便说一句,如果您真正想要的只是<code>break</code>,则不需要使用<code>stand</code>或<code>done</code>这样的标志来退出循环。你知道吗</p>
<p>我还清理了一些杂物。。。删除了一些对<code>str</code>不必要的转换,清理了检测玩家半身像、检测和打印推送等逻辑。请参阅下面的完整代码并注意区别。你知道吗</p>
<pre><code>import random
print("Welcome to PyBlackjack V1.0")
while True:
# - Reset our Hands -
dealerhand = 0
split = False
total = 0
# - Dealing the Player's hand. -
print("Dealer is dealing hand.")
firstcard = random.randrange(1, 15)
print("First card:", firstcard)
secondcard = random.randrange(1, 15)
print("Second card:", secondcard)
total = firstcard + secondcard
print("Hand:", total)
dealerfirstcard = random.randrange(1, 15)
dealerholecard = random.randrange(1, 15)
dealerhand = dealerfirstcard + dealerholecard
print("Dealer's hole card:", dealerfirstcard)
# - Player decides what to do -
while total < 21:
if not split:
print("Hand:", total)
else:
print("Left hand:", lefthand, "| Right hand:", righthand)
playerchoice = int(input("Hit (1), Double Down(2), Split(3), Stand(4)? "))
if playerchoice == 1:
total += random.randrange(1, 15)
elif playerchoice == 2:
#Reserved
break
elif playerchoice == 3:
# NOTE: This will allow splitting even after hitting
if firstcard == secondcard and not split:
lefthand = firstcard + random.randrange(1, 15)
righthand = secondcard + random.randrange(1, 15)
split = True
else:
print("You cannot split this hand!")
elif playerchoice == 4:
print("You stand.")
break
else:
print("Invalid Choice!")
print("Hand:", total)
if total > 21:
print("Bust! Game Over.")
else:
print("Dealer reveals hole card...")
print("Dealer hand:", dealerhand)
# - Dealer hits until >= 17 -
while dealerhand < 17:
print("Dealer hits...")
dealerhand += random.randrange(1, 15)
print("Dealer hand:", dealerhand)
# - Deciding who wins -
if dealerhand > 21:
print("Dealer busts! You win!")
else:
print("Your hand:", total, "| Dealer hand:", dealerhand)
if not split:
if dealerhand >= total:
print("You lose!")
elif dealerhand < total:
print("You win!")
else:
print("Push.")
else:
if lefthand > dealerhand:
print("Left hand wins!")
elif lefthand < dealerhand:
print("Left hand loses!")
else:
print("Push.")
if righthand > dealerhand:
print("Right hand wins!")
elif righthand < dealerhand:
print("Right hand loses!")
else:
print("Push.")
# - To loop or not to loop -
newgame = input("Play again? Y/N: ")
if str(newgame) == "n":
break
else:
print("Starting new game! Good Luck!")
</code></pre>