无法在python中添加数字

时间:2014-06-26 23:13:05

标签: python

我想尝试一个模拟5个骰子卷的程序。我知道有一些简单的我想念,但我不能指责它。我试图更改骰子滚动int,但它只打印输出中的数字没有添加任何内容。我做了什么???代码在这里,输出如下。任何帮助都会很棒。提前谢谢。

# This program will simulate dice rolls 5 different
# times and add the total of each roll.

import random
MIN = 1
MAX = 6
ROLLS = 5
def main():
    print(random.randint(MIN, MAX))
    print(random.randint(MIN, MAX))

    for count in range(ROLLS):
        if random.randint(MIN,MAX)== 2:
            print('The total is two!')
        elif random.randint(MIN,MAX)== 3:
            print('The total is three!')
        elif random.randint(MIN,MAX) == 4:
            print('The total is four!')
        elif random.randint(MIN,MAX) == 5:
            print('The total is five!')
        elif random.randint(MIN,MAX) == 6:
            print('The total is six!')
        elif random.randint(MIN,MAX) == 7:
            print('The total is seven!')
        elif random.randint(MIN,MAX) == 8:
            print('The total is eight!')
        elif random.randint(MIN,MAX) == 9:
            print('The total is nine!')
        elif random.randint(MIN,MAX) == 10:
            print('The total is ten!')
        elif random.randint(MIN,MAX) == 11:
            print('The total is eleven!')
        else:
            print('The total is twelve!')





        print('The dice will be rolled and the total shown')
        print('each time enter is pressed up to five times!')

        total = point(MIN, MAX)
        print('The total is', total)




def point(MIN, MAX):
    for count in range(MIN, MAX):

        dicerolls = 0.0
        total = 0.0
        dice = input('')
        total += dicerolls

        return total 



main()
2
2
The total is twelve!
The dice will be rolled and the total shown
each time enter is pressed up to five times!

The total is 0.0
The total is three!
The dice will be rolled and the total shown
each time enter is pressed up to five times!

The total is 0.0
The total is twelve!
The dice will be rolled and the total shown
each time enter is pressed up to five times!

The total is 0.0
The total is six!
The dice will be rolled and the total shown
each time enter is pressed up to five times!

The total is 0.0
The total is twelve!
The dice will be rolled and the total shown
each time enter is pressed up to five times!

The total is 0.0

2 个答案:

答案 0 :(得分:1)

每次拨打random.randint(MIN,MAX)时,都会收到一个新的随机数。

你需要做这样的事情:

for count in range(ROLLS):
    dice = random.randint(MIN, MAX)

    if dice == 2:
            print('The total is two!')

    if dice == 3:
    ....

答案 1 :(得分:0)

根据您的程序逻辑,对于每个“骰子掷骰子”,您可以循环而不为卷创建值。因此,您无法保证获得五卷。

您能看到每次拨打random.randint(MIN,MAX)的电话为每次通话产生一个新的随机数 吗?换句话说,在每次执行for count in range(ROLLS)时,在循环random.randint(MIN,MAX)内,您可以获得不同的数字,从而在没有print语句的情况下退出循环。

例如,假设这是循环随机生成的数字列表: 3,2,2,2,2,2,2,2,2,2,2,2

循环将在没有匹配的情况下完成,因为您为每个卷创建最多12次的随机数。因此,从本质上讲,如果没有更好的术语,你会得到 null roll

编辑: 同样基于MIN / MAX值,您只生成1-6的随机值,而不是1-12。

'# This program will simulate dice rolls 5 different
# times and add the total of each roll.

import random
MIN = 2  #You cannot roll a 1
MAX = 12
ROLLS = 5
def main():

    totalRolled = 0 # Accumulator

    for count in range(ROLLS)
        rolledValue = random.randint(MIN,MAX)
        print(rolledValue + " was rolled")

        if(rolledValue == 2):
             totalRolled =+ rolledValue
        if(rolledValue == 3):
             totalRolled += rolledValue

        #...Etc

        print("the total is" + totalRolled)