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 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 means it is either your answer: %r, or the only other remaining option, %r." % (youranswer, correctanswer)
if chosenstrategy == 1:
finalanswer = correctanswer
else:
finalanswer = youranswer
print "You chose %r" % finalanswer
if finalanswer == correctanswer:
wincount += 1
print "You win!"
else:
print "You lose!"
i += 1
print "You won %r out of %r games. Congrats!" % (wincount, i - 1)
def onegameatatime(doorchoice):
# for playing one Monty Hall game at a time.
playagain = 1
while playagain == 1:
possibleanswers = range(1,doorchoice + 1)
correctanswer = random.randint(1,doorchoice)
incorrectpossibilities = possibleanswers
incorrectpossibilities.remove(correctanswer)
youranswer = int(raw_input("Pick a door number from 1 - %r > " % 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)
else:
print "Which means it is either your answer: %r, or the only other remaining option, %r." % (youranswer, correctanswer)
finalanswer = int(raw_input("Which do you choose? > "))
if finalanswer == correctanswer:
print "You win!"
else:
print "You lose!"
playagain = int(raw_input("Play again? (1 = yes, 0 = no) > "))
print "Thanks for playing!"
gamechoice = int(raw_input("Play one game at a time (1) or play your chosen number of games with one strategy (2)? > "))
playchosengame(gamechoice)