patternpythonModerate
Monty Hall Simulation
Viewed 0 times
hallsimulationmonty
Problem
I wrote this code as my first self-directed effort and would appreciate any input on things that I've done seriously wrong. I worry that perhaps I'm over-relying on
```
import random
def playchosengame(chosengame):
if chosengame == 1:
doorchoice = int(raw_input("How many doors would you like to play with? > "))
onegameatatime(doorchoice)
elif chosengame == 2:
numberofgames = int(raw_input("How many games would you like to play? > "))
strategy = int(raw_input("Would you like to always change your answer (1) or keep it the same (2)? > "))
doorchoice = int(raw_input("How many doors would you like to play with? > "))
onestrategy(numberofgames,strategy,doorchoice)
else:
print "What did you say?"
def onestrategy(chosennumberofgames,chosenstrategy,doorchoice):
# for playing x number of Monty Hall games with one type of strategy
wincount = 0
i = 1
for i in range(1,chosennumberofgames + 1):
possibleanswers = range(1,doorchoice + 1)
correctanswer = random.randint(1,doorchoice)
incorrectpossibilities = possibleanswers
incorrectpossibilities.remove(correctanswer)
youranswer = random.randint(1,doorchoice)
if youranswer == correctanswer:
otherremaining = random.choice(incorrectpossibilities)
else:
otherremaining = youranswer
incorrectpossibilities.remove(otherremaining)
print "The correct answer is NOT one of these: %r" % incorrectpossibilities
if youranswer == correctanswer:
print "Which means it is either your answer: %r, or the only other remaining option, %r." % (correctanswer, otherremaining)
if chosenstrategy == 1:
finalanswer = otherremaining
else:
finalanswer = correctanswer
else:
print "Which me
if-statements, for example, but am not sure how to change the pieces of code to use them less.```
import random
def playchosengame(chosengame):
if chosengame == 1:
doorchoice = int(raw_input("How many doors would you like to play with? > "))
onegameatatime(doorchoice)
elif chosengame == 2:
numberofgames = int(raw_input("How many games would you like to play? > "))
strategy = int(raw_input("Would you like to always change your answer (1) or keep it the same (2)? > "))
doorchoice = int(raw_input("How many doors would you like to play with? > "))
onestrategy(numberofgames,strategy,doorchoice)
else:
print "What did you say?"
def onestrategy(chosennumberofgames,chosenstrategy,doorchoice):
# for playing x number of Monty Hall games with one type of strategy
wincount = 0
i = 1
for i in range(1,chosennumberofgames + 1):
possibleanswers = range(1,doorchoice + 1)
correctanswer = random.randint(1,doorchoice)
incorrectpossibilities = possibleanswers
incorrectpossibilities.remove(correctanswer)
youranswer = random.randint(1,doorchoice)
if youranswer == correctanswer:
otherremaining = random.choice(incorrectpossibilities)
else:
otherremaining = youranswer
incorrectpossibilities.remove(otherremaining)
print "The correct answer is NOT one of these: %r" % incorrectpossibilities
if youranswer == correctanswer:
print "Which means it is either your answer: %r, or the only other remaining option, %r." % (correctanswer, otherremaining)
if chosenstrategy == 1:
finalanswer = otherremaining
else:
finalanswer = correctanswer
else:
print "Which me
Solution
Welcome to Code Review and coding in general:
-
Code according to PEP8 – In your case this means using
-
Feature:
-
Use ternary related to
-
Use ternary related to print output also – This can also be used in print out as well:
-
Use the newer
-
Possibly add a loop or validation on the main loop – If you enter something when choosing between playing one game at a time, or play a chosen number of games, you can enter something wrong and the script terminates. This could possibly be enhanced by adding a while loop around it.
Do also note that if you enter non-numeric text your code breaks very easily... Look into making a general int input function which uses
-
Introducing the
In your case you could use (possibly extended with the while loop):
-
Consider shortening the text length – Your texts are rather worthy, and could benefit from being shortened a little.
Conclusion
Your code does look good for a beginner, and naming is good (with the exception of not using underscores in between words). You could benefit from learning to use the ternary operator (
-
Code according to PEP8 – In your case this means using
snake_case for variable and function names, add spaces after commas and between operators. Your code does look rather nice in general, some minor nitpicks here and there, but read through the guidelines and try to adhere to them.-
Feature:
incorrect_possibilities = possible_answers invalidates possible_answers – When you later on do incorrect_possiblities.remove(correct_answer) this also changes possible_answers. To get a copy you need either to use a better variant of copy, or simply incorrect_possibilities = possible_answers[:]. The latter one uses slicing to denote the entire array, and makes a copy of the sliced area.-
Use ternary related to
chosen_strategy - Based on the chosen strategy you choose either of two choices, this can be coded as final_answer = correct_answer if chosen_strategy == 1 else your_answer, and similar in the other cases.-
Use ternary related to print output also – This can also be used in print out as well:
print("Which means it is either your answer: {}, or the only other remaining option, {}.".format(
correct_answer if your_answer == correct_answer else your_answer,
other_remaining if your_answer == correct_answer else correct_answer))-
Use the newer
print('You chose {}'.format(final_answer) syntax – This is the preferred version for new code in Python 2 and for code in Python 3. See Format specification mini language for examples and specification.- Use docstrings,
""" ... """, and not comments,# ..., when describing modules, functions, classes and methods. – If you use docstrings they can be used by your IDE or documentation tools, and it is according to the Pythonic way. :-)
- Avoid one-time temporary variables – The
playagaincan be dropped if you usewhile True:to keep the game going, and change the end to something likeif int(raw_input("Play again (0 = no)?")) == 0: break. (Preferrably with a newline before thebreak, which does break out of the while loop)
-
Possibly add a loop or validation on the main loop – If you enter something when choosing between playing one game at a time, or play a chosen number of games, you can enter something wrong and the script terminates. This could possibly be enhanced by adding a while loop around it.
Do also note that if you enter non-numeric text your code breaks very easily... Look into making a general int input function which uses
try ... except around the input function.-
Introducing the
if __name__ == '__main__': idiom – The previous code is often used to make the code reusable as a module, and to have as little code as possible at the top level. Combined with a main() function your code easily be reused and extended.In your case you could use (possibly extended with the while loop):
def main():
game_choice = int(raw_input("Play one game at a time (1) or play your chosen number of games with one strategy (2)? > "))
play_chosen_game(game_choice)
if __name__ == '__main__':
break-
Consider shortening the text length – Your texts are rather worthy, and could benefit from being shortened a little.
Conclusion
Your code does look good for a beginner, and naming is good (with the exception of not using underscores in between words). You could benefit from learning to use the ternary operator (
a if b else c), and a slight justification on using functions related to top level code and text lengths.Code Snippets
print("Which means it is either your answer: {}, or the only other remaining option, {}.".format(
correct_answer if your_answer == correct_answer else your_answer,
other_remaining if your_answer == correct_answer else correct_answer))def main():
game_choice = int(raw_input("Play one game at a time (1) or play your chosen number of games with one strategy (2)? > "))
play_chosen_game(game_choice)
if __name__ == '__main__':
breakContext
StackExchange Code Review Q#114318, answer score: 17
Revisions (0)
No revisions yet.