Need help improving number guessing game
feba
febaen at gmail.com
Mon Dec 15 16:23:30 EST 2008
I added the ability to select your own range. It takes two new
modules:
def customrange(game, lowunsafe=True):
game['defrang'] = False #Keeps setup from changing range to
defaults
while lowunsafe: #makes sure that the low number is positive
picklow = int(input("PLEASE PICK THE LOW NUMBER: "))
if picklow < 0:
print("LOW NUMBER MUST BE POSTIVE")
else:
lowunsafe = False
pickhigh = int(input("PLEASE PICK THE HIGH NUMBER: "))
if pickhigh - picklow <= 2: #see setup().
print("HIGH MUST BE AT LEAST THREE GREATER THAN LOW")
else:
game['minr'], game['maxr'] = picklow, pickhigh
print("RANGE IS [%s-%s]!" % (game['minr'], game['maxr']))
def wantcustom(game, unsure=True):
#Allows user to decide their own range for guessing.
while unsure:
pickrange = input("WOULD YOU LIKE TO CREATE A CUSTOM RANGE? Y/
N: ")
if pickrange.lower() == "n":
game['minr'], game['maxr'] = 1, 99 #Default range
unsure = False
elif pickrange.lower() == "y":
customrange(game)
unsure = False
else:
print("INVALID INPUT")
A slightly updated setup (it needed it anyway):
def setup(game):
#minr, maxr make minimum and maximum. Can be adjusted.
#Make sure that maxr - minr is at least 3.
#1 or less would be impossible. 2 would only have one guess for
victory
#The first would be unplayable, the second would play itself
if game['maxr'] - game['minr'] <= 2:
raise ValueError("INVALID RANGE!")
game['gcount'] = 0 #Reset guess count
game['target'] = random.randint(game['minr'], game['maxr'])
and putting wantcustom(game) immediately before setup(game) in main().
More information about the Python-list
mailing list