简体   繁体   中英

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. however, if i use the python visualizer, i am only printing the first roll. how do i modify the code to print the sum of all 10 rolls?

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. Right now you are returning after the first for loop iteration ends.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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