Hi, im trying to count the number of times i roll dices of 1, 6 with x numbers of dices. But I cant understand how to import the randomlist into my next function. Can someone please explain this? I have this code, I know there is probably a much easier way of doing this but…
import random
def kast(n):
randomlist = []
for i in range(0, n):
n = random.randint(1,6)
randomlist.append(n)
print(randomlist)
return(randomlist)
def finn_antall(randomlist):
terninger = randomlist(n)
antall_1 = 0
antall_2 = 0
antall_3 = 0
antall_4 = 0
antall_5 = 0
antall_6 = 0
for num in terninger :
if num == 1:
antall_1 +=1
elif num == 2:
antall_2 +=1
elif num == 3:
antall_3 +=1
elif num == 4:
antall_4 +=1
elif num == 5:
antall_5 +=1
elif num == 6:
antall_6 +=1
print()
n = int(input('Hvor mange terninger vil du kaste? '))
kast(n)
finn_antall(kast)
There are at least two ways to do that. One is to assign result returned by the kast function to variable, that’s later passed as argument when finn_antall is called. Alternatively, if it won’t make code less readable, as argument of the finn_antall function could be used call to kast function, it will be evaluated first and result of it will be passed to other function directly.
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.