1

Write a function collect_sims(nsim,N,D,p=0.5,nmax=10000) that runs your run_sim function nsim times (with parameters N, D, p) and returns a numpy array of length nmax giving the number of times that the simulation took a specified number of steps to stop. For example, suppose nsim was 8 and successive runs of run_sim gave you 3,4,4,3,6,5,4,4. You would tabulate this as “two 3s, four 4s, one 5, one 6, zero 7s, zero 8s …”

def collect_sims(nsim, N, D, p=0.5, nmax=10000):
    run_sim(N=20, D=6, p=0.5, itmax=5000)
    onecount = 0
    twocount = 0
    threecount = 0
    fourcount = 0
    fivecount = 0
    sixcount = 0
    for k in range (n):
        if D == 1:
            onecount += 1
        if D == 2:
            twocount += 1
        if D == 3:
            threecount += 1
        if D == 4:
            fourcount += 1
        if D == 5:
            fivecount += 1
        if D == 6:
            sixcount += 1

return(k)

print(onecount, "1",twocount,"2",threecount,"3",fourcount,"4",fivecount,"5",sixcount,"6")

It says my 6 variables onecount, twocount, etc are not defined, how can I define them? Also, what can I do to fix my code?

Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
Sam
  • 11
  • 2

2 Answers2

1

I don't know why are you returning k.

Anyway, the problem is that oncount, twocount, ... etc is in different scope that print. You can put the print() inside the function or you can return an tuple with the counts

Some like that:

def collect_sims(nsim, N, D, p=0.5, nmax=10000):
    run_sim(N=20, D=6, p=0.5, itmax=5000)
    onecount = 0
    twocount = 0
    threecount = 0
    fourcount = 0
    fivecount = 0
    sixcount = 0
for k in range (n):
    if D == 1:
       onecount += 1
    if D == 2:
       twocount += 1
    if D == 3:
       threecount += 1
    if D == 4:
       fourcount += 1
    if D == 5:
       fivecount += 1
    if D == 6:
       sixcount += 1

return(onecount, twocount, threecount, fourcount,fivecount,sixcount)

onecount, twocount, threecount, fourcount,fivecount,sixcount = collect_sims (...)

print(onecount, "1",twocount,"2",threecount,"3",fourcount,"4",fivecount,"5",sixcount,"6")

Different Solution

Maybe this other solution can help you:

https://stackoverflow.com/a/9744274/6237334

Community
  • 1
  • 1
Emmanuel Arias
  • 484
  • 4
  • 11
0

Indent your for loop: in the code you posted, it's back at the original indentation level (none, for the for statement). This ends your function, and the loop is in the main program. Your variables aren't defined yet (since they're not the same as the ones in the function), and your return is illegal.

Try this, perhaps?

def collect_sims(nsim, N, D, p=0.5, nmax=10000):
    run_sim(N=20, D=6, p=0.5, itmax=5000)
    onecount = 0
    twocount = 0
    threecount = 0
    fourcount = 0
    fivecount = 0
    sixcount = 0
    for k in range (n):
        if D == 1:
            onecount += 1
        if D == 2:
            twocount += 1
        if D == 3:
            threecount += 1
        if D == 4:
            fourcount += 1
        if D == 5:
            fivecount += 1
        if D == 6:
            sixcount += 1

    print(onecount, "1",twocount,"2",threecount,"3",fourcount,"4",fivecount,"5",sixcount,"6")

collect_sims()

I can't test, since you didn't supply enough code. Also, note that I simply left the print statement in place as a debugging trace. You have to return an array, and you've made no attempt to do that yet. Your original code returned k, which had to be n+1. This is not useful to the calling program.


FURTHER HELP

Learn to use a list of 6 elements for the counts, rather than six separate variables. Even better, put all of the die rolls into a list, and simply use the count function to determine how many of each you have.

Prune
  • 76,765
  • 14
  • 60
  • 81