I have been tasked to write at least one function to simulate the rolling of dice and call that function in a loop for each player, find the total sum of dice roll though find myself heavily suffering from analysis paralysis.
sample output
How many players are rolling dice? 2
How many dice does each player roll? 3
How many sides does each die have? 6
Player 1 rolled:
4
2
4
That totals: 10
Player 2 rolled:
1
3
5
That totals: 9
My code so far
import random
#User inputs
r = int(input("How many times do you want to roll the dice? "))
s = int(input("how many sides do you want "))
p = int(input("How many players are rolling dice?"))
#Function Declaration
def Rolldice(s,r):
for i in range(0,r):
die = random.randint(1, s)
print(die)
#For loop that iterates through function declaration
for num in range(1,p 1):
print(f"player {num} rolled")
Rolldice(s,r)
print(sum(Rolldice))
Though i am receiving error listed below
TypeError: 'function' object is not iterable
CodePudding user response:
The error is caused by the last line print(sum(Rolldice))
. Rolldice
is a function, you cannot sum over function. I suppose this should solve your problem -
import random
#User inputs
r = int(input("How many times do you want to roll the dice? "))
s = int(input("how many sides do you want "))
p = int(input("How many players are rolling dice?"))
#Function Declaration
def Rolldice(s,r):
dies = []
for i in range(0,r):
die = random.randint(1, s)
print(die)
dies.append(die)
return dies
#For loop that iterates through function declaration
for num in range(1,p 1):
print(f"player {num} rolled")
dies = Rolldice(s,r)
print("That totals:", sum(dies))
CodePudding user response:
The problem comes from your last line:
print(sum(Rolldice))
You are actually passing a function to the sum()
function. But if you check the documentation for sum(), it actually requires an iterable to be passed as an argument.
You should actually try to sum the results of your dice direcly in the RollDice
function, and return the result.
def Rolldice(s,r):
total = 0
for i in range(0,r):
die = random.randint(1, s)
print(die)
sum = die
return total
Now, you can finally print the total in your main function:
for num in range(1,p 1):
print(f"player {num} rolled")
total = Rolldice(s,r)
print(f"That totals: {total}")
CodePudding user response:
You're cannot iterate over a function. However, you can iterate over it's results.
The first step to fix the code is to call the function and pass its results to sum
instead of passing the function itself.
(You do call the function in the previous line but you don't use it's result in sum
. This line could be removed.)
Replace print(sum(Rolldice))
with print(sum(Rolldice(s, r)))
.
Another problem is that you don't return value from the function. You just print it on the screen with print
.
Normally returning a value is done via the keyword return
but in this case you want to return multiple values. You could create an array of all the values and return it. It is nicely shown in the Vishwas Chepuri's answer.
However, in this case, you could use a generator. In simple words, it's a type of function that can be iterated over and return multiple values.
You can convert a normal function into a generator by returning value via yield
keyword instead of return
. In opposite to return
, yield
doesn't end the function.
Replace print(die)
with yield die
.
import random
#User inputs
r = int(input("How many times do you want to roll the dice? "))
s = int(input("how many sides do you want "))
p = int(input("How many players are rolling dice?"))
#Generator Declaration
def Rolldice(s,r):
for i in range(0,r):
die = random.randint(1, s)
yield die
#For loop that iterates through function declaration
for num in range(1,p 1):
print(f"player {num} rolled")
generator = Rolldice(s,r)
print(sum(generator))
BTW, you can write range(r)
, instead of range(0,r)
. It means the same.