當流量錯誤我是一個Python的新手,但我想我的手在寫一個腳本酒杯。在調試和糾正所有明顯的錯誤之後,我遇到了一個我無法弄清楚的奇怪事件。酒杯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,那麼它不應該輸入一個while循環,其條件是Total <21和。這裏最大的問題是組織。你試圖把它作爲一個純粹的程序系統(逐步地將每一步寫成循環,條件,輸入,輸出)。如果要將二十一點的「部分」分解爲函數,然後按程序調用這些函數,則可以更輕鬆地看到每一步發生了什麼。 –