# Need help improving number guessing game

feba febaen at gmail.com
Sun Dec 14 11:53:10 CET 2008

```#!/usr/bin/python
#Py3k, UTF-8

import random

def setup():
#global target, guess, a, b
#a, b make minimum, maximum. Can be adjusted.
a, b = 1, 99
target = random.randint(a, b)
return a, b, target

def playerswitch(player):
#Player Switch
#if player's a witch, burn her!
if player == "P1":
player = "P2"
else:
player = "P1"
return player

def youwin(pnum, player, p1sc, p2sc):
if pnum == 1:
print("CONGRATULATIONS!")
else:
if player == "P1":
p1sc += 1
else:
p2sc += 1
end = "CONGRATULATIONS %s! SCORE -- P1:%s P2:%s"
print(end %(player, p1sc, p2sc))
return p1sc, p2sc

def playagain(play):
playover = input("PLAY AGAIN? Y/N: ")
if playover.strip().lower() == "y":
play = 1
a, b, target = setup()
else:
print("GOOD BYE. PLAY AGAIN SOON!")
quit()
return play, a, b, target

def guesscheck(guess, target, play, a, b, p1sc, p2sc):
if guess == target:
p1sc, p2sc = youwin(pnum, player, p1sc, p2sc)
play, a, b, target = playagain(play)
elif guess >= b:
print("NUMBER MUST BE IN RANGE")
guess = int(input("[%s-%s]%s>> " % (a, b, player)))
play, a, b, target, p1sc, p2sc = guesscheck(guess, target,
play,
a, b, p1sc, p2sc)
elif guess <= a:
print("NUMBER MUST BE IN RANGE")
guess = int(input("[%s-%s]%s>> " % (a, b, player)))
play, a, b, target, p1sc, p2sc = guesscheck(guess, target,
play,
a, b, p1sc, p2sc)
elif guess > target:
print("TOO HIGH")
b = guess
else:
print("TOO LOW")
a = guess
return play, a, b, target, p1sc, p2sc

def guessing(a, b, player, pnum, target, p1sc, p2sc, play):
#a and b are to keep the user aware of min/max
guess = int(input("[%s-%s]%s>> " % (a, b, player)))
play, a, b, target, p1sc, p2sc = guesscheck(guess, target, play,
a, b, p1sc, p2sc)
if pnum == 2:
player = playerswitch(player)
return play, a, b, player, target, p1sc, p2sc

#Let the show begin!
print("WELCOME TO THE SUPER NUMBER GUESSING GAME!")
pnum = int(input("1 OR 2 PLAYERS?\n> "))
play = 1
player = "P1"   # P1 goes first
#Scores, keep track of times player guessed first.
p1sc, p2sc = 0, 0

#Grabs minimum, maximum, and target numbers
a, b, target = setup()

while play == 1:
play, a, b, player, target, p1sc, p2sc \
= guessing(a, b, player, pnum, target, p1sc, p2sc, play)

This is what I have now. It seems to work perfectly, and I tried to
work in your suggestions. Thanks, by the way, for recommending the
style guide. I've bookmarked it, and I'll try to remember to consult
it in the future.

One (extremely minor) annoyance is that if you have only one possible
guess left you still have to type it in manually. As an example,
[30-32]P#>> can only possibly be 31 (and I've changed it so that any
number >= 32 or <= 30 won't be accepted, so it's all the user can
input), which means whatever player is in control has a %100 chance of
winning. I know that "if b - a == 2" would be a simple enough test for
this, but I'm not quite sure when to put it, or what to do with it.

And yes, I'm aware that

guess = int(input("[%s-%s]%s>> " % (a, b, player)))
play, a, b, target, p1sc, p2sc = guesscheck(guess, target, play,
a, b, p1sc, p2sc)

is repeated three times. I will probably try to change this into a
function later on; right now, I just spent a few hours trying to get
this to work and making sure that it does (by playing it), so I'm
going to take a break.

```