Today i created a simple program that simulates the Monty Hall paradox that i learned about here:
http://betterexplained.com/articles/understanding-the-monty-hall-problem/
It took me about two class periods (about an hour and a half) and helped me teach myself about using and defining functions. It was a cool project and didn't take very long to get working. I'd like to hear feedback on how i did as i am still a beginner! Here's the code:
[source lang="python"]# Monty Hall paradox game #import pygameimport random#variablesdoors = [1, 2, 3]x = random.randint(1, 3)#functiondef show(): print '=================' print '|',doors[0],'| |', doors[1],'| |',doors[2],'|' print '================='def reveal(): if x == 1: doors[0] = 'C' doors[1] = 'G' doors[2] = 'G' show() elif x == 2: doors[0] = 'G' doors[1] = 'C' doors[2] = 'G' show() elif x == 3: doors[0] = 'G' doors[1] = 'G' doors[2] = 'C' show()def switch(): y = str(raw_input("Would you like to switch doors?")) if y == 'yes': new_pick = int(raw_input('Which door?')) user_door = new_pick reveal() elif y == 'no': reveal() show()user_door = int(raw_input('Pick a door!'))#logicif user_door == 1 and x == 3 and x != 2: doors[1] = 'G' show() switch() elif user_door == 1 and x == 2 and x != 3: doors[2] = 'G' show() switch()elif user_door == 2 and x == 3 and x != 1: doors[0] = 'G' show() switch()elif user_door == 2 and x == 1 and x != 3: doors[2] = 'G' show() switch()elif user_door == 3 and x == 1 and x != 2: doors[1] = 'G' show() switch()elif user_door == 3 and x == 2 and x != 1: doors[0] = 'G' show() switch()[/source]
K1NNY
Member Since 06 Sep 2011Offline Last Active Mar 02 2013 10:23 AM

Find content
Not Telling