在函数diceresult
中,这三个print
语句分别获得一个数字。如果你想将它们加起来而不是加上另外三个随机数,你可以使用变量来存储这三个数字,并进行求和操作。
以下是一个示例代码:
import random
def diceresult():
# 生成三个随机数
num1 = random.randint(1, 6)
num2 = random.randint(1, 6)
num3 = random.randint(1, 6)
# 打印这三个数字
print(num1)
print(num2)
print(num3)
# 将这三个数字相加
total = num1 + num2 + num3
# 打印总和
print("总和:", total)
# 调用函数
diceresult()
在这个示例代码中,我们使用random.randint()
函数生成三个随机数,并将它们分别存储在num1
、num2
和num3
变量中。然后,我们使用print
语句分别打印这三个数字。接下来,我们将这三个数字相加,并将结果存储在total
变量中。最后,我们使用print
语句打印出总和。
希望这个示例能帮助你解决问题。如果你有任何其他问题,请随时提问。
领取专属 10元无门槛券
手把手带您无忧上云