简体   繁体   English

功能中的 10 个骰子的总和不起作用? 任何返回的第一卷

[英]Sum roll of 10 dice in function not working? ony returning first roll

I am working on an assignment in which the first step is to create a function, roll_ten_dice() that gives the sum of 10 rolls of a 6 sided die.我正在做一项作业,其中第一步是创建一个函数 roll_ten_dice(),它给出 6 面骰子的 10 卷总和。 however, if i use the python visualizer, i am only printing the first roll.但是,如果我使用 python 可视化工具,我只会打印第一卷。 how do i modify the code to print the sum of all 10 rolls?我如何修改代码以打印所有 10 卷的总和?

1   import random
2   
3   def roll_ten_dice():
4       sum = 0
5       for i in range(0,10):
6           sum += random.randint(1,6)
7           return sum
8   print(roll_ten_dice())

I have tried moving the print statement inside the function, but that seems to break the code我尝试在函数内移动打印语句,但这似乎破坏了代码

You need to move your return sum outside the for loop and bring it at the same level as your function.您需要将返回总和移到 for 循环之外,并将其置于与函数相同的级别。 Right now you are returning after the first for loop iteration ends.现在,您将在第一次 for 循环迭代结束后返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM