Jump to content

  • Log In with Google      Sign In   
  • Create Account


A Monty Hall Paradox simulation program


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 K1NNY   Members   -  Reputation: 163

Like
1Likes
Like

Posted 27 September 2012 - 11:27 AM

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]

Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 12361

Like
0Likes
Like

Posted 27 September 2012 - 12:01 PM

At first glance, this seems the most unusual thing in your code:

if user_door == 1 and x == 3 and x != 2:


What does the "x != 2" part do? If x is 3, of course it is not 2, you don't need to check that.

#3 yewbie   GDNet+   -  Reputation: 665

Like
0Likes
Like

Posted 27 September 2012 - 12:18 PM

I read that as Monty [Python] Hall Paradox (silly me).

I am not familar with python but instead of doing
show() and switch() in each elif statement you could just do it at the end a single time.

Unless python doesn't allow you to do that, in which case ignore me =)

#4 Álvaro   Crossbones+   -  Reputation: 12361

Like
1Likes
Like

Posted 27 September 2012 - 02:08 PM

Yes, of course python allows that, and the suggestion is sound.

Also, what do you want to happen if I pick the correct door to begin with? Your program seems to just end in that case.

#5 ifthen   Members   -  Reputation: 820

Like
1Likes
Like

Posted 27 September 2012 - 02:35 PM

I would recommend you to make the door numbering to start from zero. This way, you are mixing door numbers (starting from 1) and array indexes (starting from 0). That makes your program very confusing. Alvaro is right, too.

Also, you should move the hard work of choosing the third door to the computer. Your "wall of elif" can be written like
[source lang="python"]if (user_door == x) #participant has chosen the right door, so you can choose either one unselected #let's go with the first door as default and the second as fallback #note that's not entirely correct, we should use the random function, but serves the purpose #if you have the time, try to replace it to use the random function if (x != 0) goat_door = 1; else goat_door = 2; else #one door has car in there and another is selected, the presenter opens the third #we iterate over the doors and find that one for test_door in [1,2,3] if user_door != test_door and car_door != test_door #we mark the door goat_door = test_door #and since it's marked, we don't need to iterate over another one break#KLUDGE: Going from one-based numbering to zero-based by subtracting onedoors[goat_door-1] = 'G'show()switch()[/source]
It probably does the same thing, but the readability is greatly improved. That reduces chance of making an error. And that will save your 2 hours of "why is this not working right" someday.Posted Image

P.S.: Use comments. Even a little bit really helps.

#6 ShadowValence   Members   -  Reputation: 379

Like
0Likes
Like

Posted 27 September 2012 - 04:42 PM

I don't know Python at all; But I agree with ifthen:

P.S.: Use comments. Even a little bit really helps.


As a beginner: I comment to the point that I feel like I'm over-commenting; but I'm not. I can't count how many times I had to revisit a code file a week or month down the road and had to sit and stare at it wondering what the cob I was thinking when I wrote it. At the time, I probably thought it was a brilliant execution. But it isn't anymore. And my error is probably somewhere in those lines. Things would be a lot easier if I knew what I was thinking at 0300hrs that morning when I typed it... :/

So comment and comment some more. And Be descriptive.

-Shadow

#7 Bacterius   Crossbones+   -  Reputation: 8276

Like
0Likes
Like

Posted 27 September 2012 - 04:48 PM

As a beginner: I comment to the point that I feel like I'm over-commenting; but I'm not. I can't count how many times I had to revisit a code file a week or month down the road and had to sit and stare at it wondering what the cob I was thinking when I wrote it. At the time, I probably thought it was a brilliant execution. But it isn't anymore. And my error is probably somewhere in those lines. Things would be a lot easier if I knew what I was thinking at 0300hrs that morning when I typed it... :/

I comment almost every single line in my code, and then filter out useless stuff later. I find it works well. I just don't get the hero coder attitude where people would write cryptic uncommented code that "just works". If you don't understand your own code, you probably should try to.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#8 ShadowValence   Members   -  Reputation: 379

Like
0Likes
Like

Posted 27 September 2012 - 06:18 PM

ero coder attitude


HAHA! I had no idea that it had a name! :D




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS