【问题标题】:Python program that simulates rolling a 6 sided die and adds up the result of each roll till you roll a 1模拟滚动 6 面骰子并将每次滚动的结果相加直到滚动 1 的 Python 程序
【发布时间】:2019-03-05 11:17:12
【问题描述】:

这是我写的试图回答标题中问题的代码:

import random
print("Well, hello there.")
while True:
    a = random.randint(1,6)
    sum = 0
    if(a==1): #If a one is indeed rolled, it just shows rolled a 1 and 'Pigs out' and makes the score equal to 0(player score that is) and is a sort of a GameOver
        print("Pigged out!")
        break #To get out of the loop
    else:
        while(sum<=20): 
            sum += a
            print(sum)

程序应将分数保持到 20(或更多)并显示它。它本质上代表“猪”的单圈。我无法弄清楚我哪里出了问题?任何建议都会有所帮助。

示例输出示例:

-rolled a 6 -rolled a 6 -rolled a 5 -rolled a 6 -Turn score is 23

【问题讨论】:

    标签: python python-3.x probability dice


    【解决方案1】:

    如果您只想在总和大于 20 时显示总和,您不应该将 print(sum) 向左缩进吗?本质上:

    while(sum<=20): 
        sum += a
    print(sum)
    

    如果您能阐明您希望输出是什么以及它当前在做什么,那就太好了。

    【讨论】:

    • 它应该滚动到总和为 20 或更多并显示它。现在我的程序滚动了很多非 1,并且一直持续到滚动 1。当总和为 20 时如何让它停止?
    【解决方案2】:

    总和超过20后你应该打破

    else:
        while(sum<=20): 
            sum += a
            print(sum)
        break
    

    编辑:

    import random
    print("Well, hello there.")
    while True:
        a = random.randint(1,6)
        sum = 0
        if(a==1): #If a one is indeed rolled, it just shows rolled a 1 and 'Pigs out' and makes the score equal to 0(player score that is) and is a sort of a GameOver
            print("Pigged out!")
            break #To get out of the loop
        else:
            if not SumWasReached:
               while(sum<=20): 
                   a = random.randint(1,6)
                   sum += a
                   print(sum)
               SumWasReached ==True:
            else:
                while(a!=1):
                   a = random.randint(1,6)
                break
    

    【讨论】:

    • 好吧,我尝试缩进代码并添加 break 语句,它可以工作,但它滚动相同的骰子值?喜欢它显示滚动 4 5 次或滚动 6 4 次并显示分数。是一些错误还是只是“随机”?
    • 它再次滚动数字,因为 a = random.randint(1,6) 被设置在循环之外。所以它不会再骰子了
    • @user285347 谢谢!终于想通了!
    【解决方案3】:

    这是你想要的吗?

    import random
    print("Well, hello there.")
    sum=0
    while True:
        a = random.randint(1,6)
        sum+=a
        if(a==1): #If a one is indeed rolled, it just shows rolled a 1 and 'Pigs out' and makes the score equal to 0(player score that is) and is a sort of a GameOver
            print("Pigged out!")
            break #To get out of the loop
        else:
            if sum<=20: 
                sum += a
                print(sum)
            else:
                print(sum,'limit reached')
                break
    

    【讨论】:

    • 输出应该是类似-rolled a 6 -rolled a 4 的东西,直到分数为 20 或更多,此时它应该停止。如果掷出 1,则分数变为 0(游戏结束)。我添加了一个 print("- rolling a " + str(a)) 来向用户展示他们掷出的结果。
    【解决方案4】:

    如果我理解正确,那么您可以将其简化很多,如下所示:

    import random
    print("Well, hello there.")
    score = 0
    while score < 20:
        a = random.randint(1,6)
        print("A {} was rolled".format(a))
        score += a
        if a == 1:
            print("Pigged out!")
            score = 0
            break
    print("Turn score is {}".format(score))
    

    【讨论】:

    • 是的,这也有效!我喜欢这是多么简单明了!由于我是初学者,仍在学习如何简化代码。谢谢!
    猜你喜欢
    • 2021-05-16
    • 2021-02-06
    • 2014-03-16
    • 1970-01-01
    • 2013-05-09
    • 2016-03-14
    • 2013-09-17
    • 2021-11-13
    • 1970-01-01
    相关资源
    最近更新 更多