在python中生成随机数字?

2024-09-28 20:45:00 发布

您现在位置:Python中文网/ 问答频道 /正文

每个游戏(gameA和gameB)都是随机的,每个游戏都有与玩家相对的赔率,每玩一次游戏,如果你赢了,你就赢1美元,如果你输了,你就输1美元。 我想生成一个从0到1的随机数,如果这个数小于0.4我就输,如果它越大我就赢。这是代码,但它给了我一个错误,你能帮我吗? 问题是:玩这个游戏我会损失多少钱?你知道吗

import random
def testA():
    # game A
    gameA = random.random()
    if gameA>0.4   #error
    profitA=profitA+1
    end
    return profitA
def testB():    
    #game B    
    gameB = random.random()
    if gameB>0.4
    profitA=profitA+1
    end
    return profitB
def runTests(repititions):
    sumA = 0
    sumB = 0
    for i in range(repititions):
        profitA = testA()
        profitB = testB()
        sumA += profitA
        sumB += profitB

    return sumA, sumB
repititions = int(raw_input("Repititions: "))
sumA, sumB = runTests(repititions)

print sumA
print sumB

Tags: game游戏returnifdefrandomendsuma
1条回答
网友
1楼 · 发布于 2024-09-28 20:45:00

你有两个语法错误,一个空格错误,你使用的关键字不存在。你知道吗

if gameA>0.4   #<  end this with a colon
profitA=profitA+1 #<  needs more indent
end #<  what is end?

所有这些都在gameB中重复。你知道吗

此外:

  • profitAprofitBtestAtestB中没有定义。你知道吗
  • testA是与testB相同的代码,这意味着随着时间的推移,它们将返回大约相同的值。你知道吗

相关问题 更多 >