CreamStat
CreamStat

Reputation: 2185

Python function doesn´t respond

I´m new to python. I have these four functions which are related but the last doesn´t respond.

import ui as numpy


def simulate_prizedoor(nsim):
    sim=ui.random.choice(3,nsim)
    return sims

def simulate_guess(nsim):
        guesses=ui.random.choice(3,nsim)
        return guesses

def goat_door(prizedoors, guesses):


        result = ui.random.randint(0, 3, prizedoors.size)
        while True:
            bad = (result == prizedoors) | (result == guesses)
            if not bad.any():
                return result
            result[bad] = ui.random.randint(0, 3, bad.sum())

def switch_guesses(guesses, goatdoors):


            result = ui.random.randint(0, 3, guesses.size)
            while True:
                bad = (result == guesses) | (result == goatdoors)
                if not bad.any():
                    return result
                result[bad] = ui.random.randint(0, 3, bad.sum())

As you see the third and the four function has the same code.

I run the first and the second function :

a=simulate_prizedoor(4)
b=simulate_guess(4)
print(a)
[2 2 0 2]
print (b)
[2 0 0 2]

Then, I run the third function with the values of a and b

c=goat_door(a,b)
print(c)
[1 1 2 1]

Finally, I run the last function but python didn´t respond, it´s seems that it´s a infinite process to give the answer.

   switch_guess(b,c)

Edit:

Here's the image:

enter image description here

Upvotes: 0

Views: 192

Answers (1)

Peter Gibson
Peter Gibson

Reputation: 19554

The code you have posted has errors that prevent it from running. Please post the original unmodified code when asking a question - you will be much more likely to get constructive feedback and good answers.

Here are the errors I found with the code you posted:

Traceback (most recent call last):
  File "montyhall.py", line 1, in <module>
    import ui as numpy
ImportError: No module named ui

My guess at correct line: import numpy as ui

Traceback (most recent call last):
  File "montyhall.py", line 27, in <module>
    a=simulate_prizedoor(4)
  File "montyhall.py", line 5, in simulate_prizedoor
    return sims
NameError: global name 'sims' is not defined

My guess at correct function:

def simulate_prizedoor(nsim):
    sim=ui.random.choice(3,nsim)
    return sim

Your indenting is also a bit strange.


The corrected code I ran is included below. When I run it, there are no errors and the simulate_prizedoor function does not get stuck in a loop. Perhaps the code you are running has differences that are causing the problem?

import numpy as ui

def simulate_prizedoor(nsim):
    sim=ui.random.choice(3,nsim)
    return sim

def simulate_guess(nsim):
    guesses=ui.random.choice(3,nsim)
    return guesses

def goat_door(prizedoors, guesses):
    result = ui.random.randint(0, 3, prizedoors.size)
    while True:
        bad = (result == prizedoors) | (result == guesses)
        if not bad.any():
            return result
        result[bad] = ui.random.randint(0, 3, bad.sum())

def switch_guesses(guesses, goatdoors):
    result = ui.random.randint(0, 3, guesses.size)
    while True:
        bad = (result == guesses) | (result == goatdoors)
        if not bad.any():
            return result
        result[bad] = ui.random.randint(0, 3, bad.sum())

a=simulate_prizedoor(4)
b=simulate_guess(4)
print(a)
print (b)
c=goat_door(a,b)
print(c)
d=switch_guesses(b,c)
print(d)

Upvotes: 1

Related Questions