How do you use sum and range values?

2020-05-06 07:48发布

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.

2条回答
做自己的国王
2楼-- · 2020-05-06 08:30

Do you mean by:

print("You rolled", sum(number), "dices. The numbers are", random.sample(range(50), 12), ".")
查看更多
够拽才男人
3楼-- · 2020-05-06 08:32

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:

import numpy as np

num_dice = int(input("How many dice would you like to roll? "))
dice_sides = int(input("How many sides per die? "))

while num_dice <= 0 or dice_sides <=0:
    if num_dice <= 0:
        num_dice = int(input("Invalid number of dice, enter a number greater than or equal to 1."))
    else:
        dice_sides = int(input("Invalid number of sides, enter a number greater than or equal to 1."))

rolled_nums = np.random.choice(range(1, dice_sides), size=num_dice, replace=True)
# If you don't want the brackets around the numbers produced by the dice rolling, 
# use ", ".join(str(i) for i in list(rolled_nums)) instead of list(rolled_nums) here.
print("You rolled {} dices. The numbers are {}.".format(num_dice, list(rolled_nums)))

decision = input("Would you like to sum the points for all the rolled numbers? (y/n) -> ")
if decision == 'y' or decision == 'Y':
    print("Total sum of all the rolled numbers is: {}".format(np.sum(rolled_nums)))
else:
    quit()

Some notes to make:

  1. You have to make sure that the user enters reasonable inputs (values greater than 0) or else your calculation is not defined. This is done with the while loop to avoid just a single check.
  2. Also, notice the use of format() in print() 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/
  3. If you don't want the brackets around the numbers produced by the dice rolling, use ", ".join(str(i) for i in list(rolled_nums)) instead of list(rolled_nums). This is indicated in the code with a comment

Edit: If you don't like numpy, here is the same functionality using just import random.

import random

num_dice = int(input("How many dice would you like to roll? "))
dice_sides = int(input("How many sides per die? "))

while num_dice <= 0 or dice_sides <=0:
    if num_dice <= 0:
        num_dice = int(input("Invalid number of dice, enter a number greater than or equal to 1."))
    else:
        dice_sides = int(input("Invalid number of sides, enter a number greater than or equal to 1."))

# Roll the dice one at a time and store their values
rolled_nums = []
for _ in range(num_dice):
  rolled_nums.append(random.choice(range(1, dice_sides)))

print("You rolled {} dices. The numbers are {}.".format(num_dice, rolled_nums))

decision = input("Would you like to sum the points for all the rolled numbers? (y/n) -> ")
if decision == 'y' or decision == 'Y':
    print("Total sum of all the rolled numbers is: {}".format(sum(rolled_nums)))
else:
    quit()

Again, you can remove the brackets from the list of numbers using the method described above (point 3.)

查看更多
登录 后发表回答