将两个随机数加在一起

时间:2017-11-21 23:47:41

标签: python random addition

因此我无法将2个随机生成的数字加在一起。用户必须看到生成的两个随机数,然后计算机必须将它们一起添加并打印出来。任何人都可以告诉我我做错了什么?

import random
import time

roll_again = "yes"

while roll_again == "yes" or roll_again == "y":
    min = 1
    max = 6
    print("Rolling the dices...")
    time.sleep(2)
    print("The values are...")
    time.sleep(2)
    dice1 = print(random.randint(min, max))
    dice2 = print(random.randint(min, max))

    usertotal = dice1 + dice2

1 个答案:

答案 0 :(得分:0)

在您的代码中,您要在终端上打印随机数,不要将它们存储在变量dice1和dice2中。您的代码无法正常工作。

dice1 = print(random.randint(min, max))
dice2 = print(random.randint(min, max))
usertotal = dice1 + dice2

相反,你应该这样做:

dice1 = random.randint(min, max)
print(dice1)
dice2 = random.randint(min, max)
print(dice2)
usertotal = dice1 + dice2
print(usertotal)