【问题标题】:Generate random numer in python?在python中生成随机数?
【发布时间】:2013-11-13 13:07:47
【问题描述】:

每场比赛(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

【问题讨论】:

  • 问题是什么?
  • 玩这个游戏我损失了多少钱?
  • 您需要在提问时添加准确的错误消息。此外,if 语句的语法看起来不正确。尝试谷歌搜索“Python 条件语句”。
  • 此外,检查随机返回的类型。
  • @TudorPlugaru 读了一本 Python 书。您的代码表明您对 Python 语法的了解为零。

标签: python


【解决方案1】:

您有两个语法错误,一个空格错误,并且您使用的关键字不存在。

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

所有这些都在gameB 中重复。

另外:

  • profitA 和 profitB 未在 testA 和 testB 中定义。
  • testA 与 testB 的代码相同,这意味着随着时间的推移,它们将返回大致相同的值。

【讨论】:

    猜你喜欢
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多