Def function help

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.

Hello there,

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.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

What do you mean, assign result returned by kast?

Result of the function call can be assigned to the variable.

Can you elaborate? I cant seem to bring with me the randomlist down to the next function. All I keep on doing is creating a new list.

I kind of understand what u mean, I just cant seem to work.

Taking example from the code:

 n = random.randint(1,6)

Result returned by the function on the right is assigned to the variable n, which can be later used by that name.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.