I would like to know how to sum and use range values in Python.
Let's say I want to use something like this:
You rolled 12 dices. The numbers are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11.
Would you like to score the points for all different numbers (50 points)?
And how would I score the points for all numbers?
I know about for n in range(12):
and random.randint(1,12)
but I am not very good in Python and would like some help with this. Thank you in advance.
Do you mean by:
I think this is a good task with numpy.
First you should decide how many dice they want to use and how many edges they want to have on each die. Then you should roll the dice and get output for each die according to the number of edges (there will be repetition to simulate real world scenario). Lastly, you ask if they want to sum and if so do it for them using
np.sum()
.Full code:
Some notes to make:
format()
inprint()
which helps keep your commands cleaner. I highly recommend using it as with more complex code, it will be a life saver. Check it out here for more details: https://www.geeksforgeeks.org/python-format-function/", ".join(str(i) for i in list(rolled_nums))
instead oflist(rolled_nums)
. This is indicated in the code with a commentEdit: If you don't like numpy, here is the same functionality using just
import random
.Again, you can remove the brackets from the list of numbers using the method described above (point 3.)