Python逻辑Black Jack sim

时间:2017-02-05 02:11:21

标签: python

我无法弄清楚我在哪里犯了错误,但打印总是可能胜利100%

setInterval(function() {
   var thisyearval = parseInt($("#thisyear").text(),10)
   var lastyearval = parseInt($("#lastyear").text(),10)


   if (thisyearval > lastyearval){
      $("#wrapper").css("background-color", "#009900")
   } else{$("#wrapper").css("background-color", "#FF0000")}
}, 500);

设置牌组并检查单场比赛的得分

import random

检查是赢,输还不是

def singleGame():
    cardsVal = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] # deck of cards
    onhandScore = 17 # score on hand
    cardNumber = pickOneCard()
    newCardValue = cardsVal[cardNumber - 1]
    onhandScore = onhandScore + newCardValue

选择一张13张牌并退回其卡号

if onhandScore > 21:
    result = 1
elif onhandScore == 21:
    result = 0
else:
    result = -1

return result

检查n个模拟并打印结果

def pickOneCard():
    return random.randint(1, 13)

记录分数

def multipleGames(n):

逐个模拟n个游戏

defeat = 0;
certainVictory = 0;
likelyVictory = 0;

检查分数和存储值

for i in range(n):
    result = singleGame()

计算总百分比

if result == 1:
    defeat += 1
elif result == 0:
    certainVictory += 1
else:
    likelyVictory += 1

打印结果

totalPer = (defeat + certainVictory + likelyVictory)
defeatPer = float("{0:.2f}".format(defeat * 100/ totalPer))
certainVictoryPer = float("{0:.2f}".format(certainVictory * 100/ totalPer))
likelyVictoryPer = 100 - defeatPer - certainVictoryPer

模拟100,000场比赛的代码

print 'Defeats: ',defeatPer,'%'
print 'Certain victories: ',certainVictoryPer,'%'
print 'Likely victories: ',likelyVictoryPer,'%'

1 个答案:

答案 0 :(得分:2)

检查你的缩进以检查胜利,失败或平局。你的return语句应该在if语句之外,如此。

if onhandScore > 21:
    result = 1
elif onhandScore == 21:
    result = 0
else:
    result = -1

return result

因此,只有在达到else情况时才返回整数值。否则,该函数默认返回None。现在我们到达这里。

if result == 1:
    defeat += 1
elif result == 0:
    certainVictory += 1
else:
    likelyVictory += 1

None不等于10,因此它会到达else,当它return -1时,它也会为likelyVictory添加一个ActionContext.getContext() }。