總結問題
我正在嘗試制作一個簡單的二十一點程式,它為莊家 1-11 選擇兩個數字,也為玩家 1-11 選擇兩個數字。我想創建一個回圈,根據玩家輸入、擊中或停留的內容來檢查玩家是擊中還是停留。如果他們擊中,我希望它向玩家牌組添加一張牌,并讓他們選擇擊中或再次停留。
如果他們留下來,我希望經銷商檢查一組規則,這些規則是:
1:莊家的點數是否小于 17 且是否小于玩家 1a:如果它的點數小于 17 且小于玩家,它將抽取一個新數字 1-11 并將其添加到莊家手中 1b:它然后將運行檢查它是否具有以下條件的回圈
2:莊家是否有 17-21 并且是否比玩家多
3:莊家是否超過 21 3a:如果超過 21 則它會列印房屋破壞訊息
4:莊家是否比玩家多 4a:如果莊家比玩家多(但由于之前的檢查而少于 21),那么它會列印一個房子獲勝的訊息
所以這就是我試圖用stay 命令完成的事情,到目前為止我還沒有發現任何問題。
然而,我的命中命令不會注冊,即使我不輸入stay ..認為這可能是我的輸入轉換為字串的問題。
不要對我太苛刻,哈哈,我上周才自學了如何編碼,并從 python 開始。
這是我遇到問題的代碼:
'''
else:
hitstay = True
while hitstay:
action = str(input("hit or stay? "))
dealer = sum(dealer_cards)
player = sum(player_cards)
if action == 'stay' or 'Stay':
if dealer < 17 and dealer < player:
dealer_cards.append(random.randint(1, 11))
dealer = sum(dealer_cards)
print(f"Dealer pulls {dealer_cards[-1]}\n"
f"Dealer now has {sum(dealer_cards)}")
if 17 <= dealer <= 21 and dealer > player:
print(f"House won! Dealer cards: {dealer_cards}, {sum(dealer_cards)}\n"
f"Player cards: {player_cards}, {sum(player_cards)}")
playing = False
hitstay = False
elif dealer > 21:
print(f'House busted! Dealer cards: {dealer_cards}')
playing = False
hitstay = False
elif dealer > player:
print(f'House wins, Dealer cards {dealer_cards}, {sum(dealer_cards)}\n'
f'Player cards: {player_cards}, {sum(player_cards)}')
playing = False
hitstay = False
if action == 'hit' or 'Hit':
print('command hit')
playing = False
hitstay = False
'''
uj5u.com熱心網友回復:
代替:
if action == 'stay' or 'Stay':
做:
if action == 'stay' or action == 'Stay':
或者:
if action in ('stay', 'Stay'):
或者最重要的是:
if action.lower() == 'stay':
第一個版本不起作用,因為它被解釋為:
if (action == 'stay') or 'Stay':
這與:
if (action == 'stay') or True:
這與:
if True:
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/384795.html
上一篇:在for回圈中為變數賦值
