# Need help improving number guessing game

feba febaen at gmail.com
Sat Dec 13 16:21:59 CET 2008

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

import random

def startup():
print("WELCOME TO THE SUPER NUMBER GUESSING GAME!")
global pnum, play, player, p1sc, p2sc
pnum = int(input("1 OR 2 PLAYERS?\n> "))
play = True
player = "P1" #P1 goes first
p1sc = 0 #Number of times...
p2sc = 0 #player guessed before opponent

def setup():
global target, guess, a, b
a = 1
b = 99
target = random.randint(a, b)
guess = 0 #Won't fall between 1 and 99

def playerswap():
global player
if player == "P1":
player = "P2"
else:
player = "P1"

def guessing():
global guess, player, target, play, pnum, p1sc, p2sc, a, b
guess = int(input("[%s-%s]%s>> " % (a, b, player))) #keeps the
user aware of min/max
if guess == target:
if pnum == 1:
print("CONGRATULATIONS!" )
else:
if player == "P1":
p1sc += 1
else:
p2sc += 1
print("CONGRATULATIONS %s! SCORE -- P1:%s P2:%s" %(player,
p1sc, p2sc))

playover = input("PLAY AGAIN? Y/N: ")
if playover.strip().lower() == "y":
play = True
setup()
else:
play = False
elif guess > b:
print("NUMBER MUST BE IN RANGE")
elif guess <= a:
print("NUMBER MUST BE IN RANGE")
elif guess > target:
print("TOO HIGH")
b = guess
else:
print("TOO LOW")
a = guess
if pnum ==2:
playerswap()

startup()
setup()

while play is True:
guessing()

This is what I have so far. better? worse? I'm guessing a mix of the
two. It took me a lot longer to get working, but I think it works
better. I also added a bit that tells you if you go higher or lower
than an already specified too high/low markers; although it doesn't
make you repeat that turn. I'm not sure if all those 'globals' are
bad, but they don't seem like they're something that should be good.
Functionally, it seems to work just fine.

```