在 Python 中循环访问系统参数



这是一个二十一点程序。当 stand 函数为 false 时,main 函数不应该将 getCard(( 附加到 myCards,但由于某种原因它仍然这样做并且它向后计数。我不知道我做错了什么。

import random
import sys
def get_card():
return random.randint(1, 13)
def score(cards):
soft_ace_count = 0
total = 0
Ace = False

检查手中是否存在 Ace 并将面牌设置为正确的值

for card in cards:
if card == 1:
Ace = True
total+=11
soft_ace_count+=1
elif card == 11 or card == 12 or card == 13:
total+=10
else:
total+=card

转换 Ace 的步骤

for x in cards:
if Ace and total > 21:
total-=10
soft_ace_count-=1
return (total, soft_ace_count)
def stand(stand_on_value, stand_on_soft, cards):
total, soft_ace_count = score(cards)
print(f"In stand: {total}")
if total > 17 and total < 22:
return True
if total == stand_on_value:
return True
elif soft_ace_count == 0 and total == 17:
return True
elif stand_on_soft == True and total == 17:
return True
else:
return False
def numBusts(s):
total, soft_ace_count = s
busted_count = 0
if total > 21:
busted_count+=1
return busted_count
def main():
numSims = int(sys.argv[1])-1
standVal = int(sys.argv[2])
strategy = sys.argv[3]
strategy.upper()
for sims in range(numSims+1):
percent_bust = 0.0
myCards = [get_card(), get_card()]
print(f"in main: first two cards: {myCards}")
stand(standVal, strategy, myCards)
while not stand(standVal, strategy, myCards):
myCards.append(get_card())
percent_bust = (numBusts(score(myCards))/(numSims+1)) * 100
print(f"in main: percent bust: {percent_bust}")
if __name__ == "__main__":
[The image shows how it runs correctly until the while not stand function returns False.][1]main()

我试过运行循环,不运行循环和其他事情。

请帮忙。

当站立为假时,while not stand将附加到 get_card(( 的结果到 myCards。

但是在你的问题中,你说你不希望它在stand((为假时附加。

另外,您似乎连续两次调用支架函数,这是有意的吗?条件实际上调用函数以检查其结果。

也许这会做你所期望的?

for sims in range(numSims+1):
percent_bust = 0.0
myCards = [get_card(), get_card()]
print(f"in main: first two cards: {myCards}")
if stand(standVal, strategy, myCards) is True:
myCards.append(get_card())

当且仅当stand()返回 True 时,此条件才会附加到 myCards,但当返回 False、0 或 None 时不会

最新更新