[英]How can I fix the scoring on my blackjack game?
我目前遇到一個問題,我在我創建的代碼上玩了一輪二十一點,但一直輸。 我的分數將小於 21,但仍高於庄家,我會輸。 我對編碼很陌生,所以感謝您的幫助,謝謝。
def FinalScore():
global bank, bet
# different win conditions
# pays the player their original bet * 2
if player_score == dealer_score and player_score <= 21:
print("It's a tie!")
bank = bank + bet
print("You currently have $",bank,"left.")
Restart()
elif player_score > 21:
print("You lost!")
print("You currently have $",bank,"left.")
Restart()
elif player_score < 21 and dealer_score > player_score:
print("You lost!")
print("You currently have $",bank,"left.")
Restart()
elif player_score > dealer_score and player_score <= 21:
print("You win!")
bank = bet + bet + bank
print("You currently have $",bank,"left.")
Restart()
elif dealer_score > 21 and player_score <= 21:
print("You win!")
bank = bet + bet + bank
print("You currently have $",bank,"left.")
Restart()
我嘗試重新排列獲勝條件的順序,它確實改變了一些結果,但最終它仍然是 finnicky。 我認為有一種我不知道的更好的方法可以做到這一點。
你有很多條件; BJ里,燒了你就輸了(>21); 但在那之后,如果庄家燒錢,你就贏了; 在您與庄家的分數比較后;
def FinalScore():
global bank, bet
# different win conditions
# pays the player their original bet * 2
if player_score > 21:
print("You lost!")
else:
if dealer_score > 21:
print("You win!")
bank = bank + 2*bet
elif player_score == dealer_score:
print("It's a tie !")
bank = bank + bet
elif player_score < dealer_score:
print("You lost!")
else:
print("You win!")
bank = bank + 2*bet
print("You currently have $",bank,"left.")
Restart()
問題是你使用 if 條件作為“案例”,這就是 elif 條件在這里發生的事情是它會落在第一個真實的陳述上,這里elif player_score < 21 and dealer_score > player_score:
,在這個階段沒有檢查可能大於 21 的 dealer_score 的值。為了更具可讀性,首先嘗試定義當玩家高於 21 時會發生什么,如下所示:
if dealer_score > 21:
print('win')
return Restart()
if player_score > 21 :
print('loose')
return Restart()
else :
if player_score > dealer_score :
print('win')
elif player_score == dealer_score:
print('tie')
else:
print('loose')
return Restart()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.