tryingtolearnthings - 2 years ago 56
Python Question

# beginner python: monty hall & counter output

I am trying to count the number of times a person who changes their door choice would be likely to win (get the car) out of 10,000 iterations. I'm not sure whether my range or counter is formatted incorrectly (or both). The code seemed to be working until I tried to print counter - which resulted in a list of 1s.

``````for i in range (10000):

doors = ["goat"] + ["goat"] + ["car"]
rn.shuffle(doors)

persons_choice = rn.randint(0,2)
persons_choice1 = str(doors[persons_choice])

if persons_choice1 == "goat":
monty_choice = "goat"
elif persons_choice1 == "car":
monty_choice = "goat"

if persons_choice1 == "car":
doornumber3 = "goat"
elif persons_choice1 == "goat":
doornumber3 = "car"

final_options = [persons_choice1] + [doornumber3]
rn.shuffle(final_options)
final_choice = rn.randint(0,1)
thefinal_choice = str(final_options[final_choice])

counter = 0
if thefinal_choice == "car":
if str(persons_choice1) != str(thefinal_choice):
counter += 1
print counter
``````

Your range is formatted properly. You are right that where your `counter` is is the problem.

As you may know, the `for` loop runs all the code that's indented below the `for` statement, each time in the loop. You initialize your counter to 0 inside the loop, so the only time it will print, it will have been initialized to 0, then you added 1 to it with the line `counter += 1`. You should put both your initialization and your answer out of the loop. Here's an example, using the code you posted:

``````counter = 0

for i in range (10000):

doors = ["goat"] + ["goat"] + ["car"]
rn.shuffle(doors)

persons_choice = rn.randint(0,2)
persons_choice1 = str(doors[persons_choice])

if persons_choice1 == "goat":
monty_choice = "goat"
elif persons_choice1 == "car":
monty_choice = "goat"

if persons_choice1 == "car":
doornumber3 = "goat"
elif persons_choice1 == "goat":
doornumber3 = "car"

final_options = [persons_choice1] + [doornumber3]
rn.shuffle(final_options)
final_choice = rn.randint(0,1)
thefinal_choice = str(final_options[final_choice])

if thefinal_choice == "car":
if str(persons_choice1) != str(thefinal_choice):
counter += 1

print counter
``````

Note, this code still has other issues. You put some effort into calculating `monty_choice`, but it will currently always be "goat", but then you don't use `monty_choice` anyway, so it probably didn't matter.

You logic in the next `if` block is questionable as well: if persons_choice1 was car, then door 3 will definitely be a goat. But what happens if person's choice was goat, and the choice they didn't take was already the car? You can end up with two chances at a `car` in that case. Also, your final if statement is only increasing your counter when the person did not take the doornumber3 choice and won the car. I'm not sure if that's your intent or not.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download