(I mean the Monty Hall problem, this site won’t let me submit something with ‘problem’ in the title)
I’m attempting to create the Monty Hall problem in python, I thought I had a good plan, but I’m getting the error ‘”NoneType’ object is not subscriptable”. This happens when I try to nest the lists. Does anyone know how I can do this? or fix it?
Thank you.
Here is the code:
car = 0
goat = 0
turns = 0
amount = 100
import random
items = ["New Car", "Goat", "Goat"]
doors = ["Door1", "Door2", "Door3"]
while turns < amount:
x = random.shuffle(items)
door = random.shuffle(doors)
door1 = [door[0], x[0]]
door2 = [door[1], x[1]]
door3 = [door[2], x[2]]
new_list = [door1, door2, door3]
player_choice = random.randint(0, 2)
if player_choice == 0:
print("You have chosen {0}, and behind that is a {1}".format(door1[0], door1[1]))
if new_list[0][1] == "New Car":
car += 1
turns += 1
else:
goat += 1
turns += 1
And then error:
Traceback (most recent call last):
File "<string>", line 250, in run_nodebug
File "C:\Programming\Random Programs\Monty Hall Problem.py", line 16, in <module>
door1 = [door[0], x[0]]
TypeError: 'NoneType' object is not subscriptable
random.shuffledoesn’t return a result. It just modifies the list you pass as an argument.Perhaps you want something like this?