Python程序,用于模拟滚动6面骰子并累加每次滚动的结果,直到滚动1

时间:2018-09-29 21:13:34

标签: python python-3.x probability dice

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

let items = [
    {"id":"brand","label":"Brand","value":"value1"},    
    {"id":"waterproof","label":"Waterproof","value":"value1"},
    {"id":"diameter","label":"Diameter","value":""},
    {"id":"brand","label":"Brand","value":"value2"},
    {"id":"waterproof","label":"Waterproof","value":"value2"},
    {"id":"diameter","label":"Diameter","value":""}
]

let result = [...new Set(items.map(i => i.id))]
  .map(id => {
    return [
      items.find(i => i.id == id).label,
      ...items.filter(i => i.id == id).map(i => i.value)
    ]
  })

console.log(result)

程序应将分数保持到20(或更高)并显示它。它实质上代表了“猪”的一个转弯。我无法弄清楚我要怎么做?任何建议都会有所帮助。

示例输出示例:

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)

4 个答案:

答案 0 :(得分:1)

如果我的理解正确,那么您可以将其简化很多,就像这样:

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))

答案 1 :(得分:0)

如果您只想显示大于20的总和,是否不应该在左边缩进print(sum)?本质上:

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

如果您能弄清输出要是什么以及当前正在做什么,那就太好了。

答案 2 :(得分:0)

您应该在总数超过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

答案 3 :(得分:0)

这是你想要的吗?

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