Advertisement
Guest User

MHP in python

a guest
Feb 25th, 2014
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. DOORS=['A','B','C']
  2. ALL_WIN=DOORS
  3. CHOOSE=DOORS
  4.  
  5. total_wins=total_loss=0
  6. stay_win=stay_loss=0
  7. switch_win=switch_loss=0
  8.  
  9. def did_i_win(chosen_door, the_car):
  10. if chosen_door == the_car:
  11. return True
  12. else:
  13. return False
  14.  
  15.  
  16. def show_results(fc, reveal, swap, car):
  17. if swap == fc: swap = '-'
  18. print "Car: %s Choice: %s Reveal: %s Change: %s win=%s" \
  19. %(car, fc, reveal, swap, did_i_win(swap, car))
  20.  
  21. def stay(my_door, revealed, car):
  22. if did_i_win(my_door, car):
  23. global stay_win
  24. stay_win+=1
  25. global total_wins
  26. total_wins+=1
  27. else:
  28. global stay_loss
  29. stay_loss+=1
  30. global total_loss
  31. total_loss+=1
  32. show_results(my_door, revealed, my_door, car)
  33.  
  34. def switch(my_door, revealed, car):
  35. switch_list = [(g) for g in DOORS if g <> my_door and g <> revealed]
  36. # change the one-element [0] list to a string
  37. switch = switch_list[0]
  38. if did_i_win(switch, car):
  39. global switch_win
  40. switch_win+=1
  41. global total_wins
  42. total_wins+=1
  43. else:
  44. global switch_loss
  45. switch_loss+=1
  46. global total_loss
  47. total_loss+=1
  48. show_results(my_door, revealed, switch, car)
  49.  
  50. for winning_door in DOORS:
  51. for choice in CHOOSE:
  52. monty_choices = [(g) for g in DOORS if g <> winning_door and g <> choice]
  53. for reveal in monty_choices:
  54. stay(choice, reveal, winning_door)
  55. switch(choice, reveal, winning_door)
  56. print
  57. print
  58.  
  59. print "%s stay and win" % stay_win
  60. print "%s stay and lose" % stay_loss
  61. print "%s switch and win" % switch_win
  62. print "%s switch and lose" % switch_loss
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement