feba a écrit :
#!/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

Seems ok. You may want to use arguments with default values for a and b (and possibly to use more meaningfull names):

def setup(mini=1, maxi=99)
   # sanity check
   if mini >= maxi:
      raise ValueError("mini must be lower than maxi")
   target = random.randint(mini, maxi)
   return mini, maxi, target

def playerswitch(player):
    #Player Switch
    #if player's a witch, burn her!

Extra bonus point for quoting Monty Pythons !-)

    if player == "P1":
        player = "P2"
    else:
        player = "P1"
    return player


I'd name this one "switch_player" (most of the time, function names should be verbs) - but this is mostly a matter of personal taste !-)

Minor point for a short program, but still good practice : use constants. IE :

# at the top level
PLAYER_1 = 1
PLAYER_2 = 2

# in your code
....
   if player == PLAYER_1:
...


Also, you could use early returns here instead of rebinding then returning player.

def switch_player(player):
    if player == PLAYER_1:
        return PLAYER_2
    else:
        return PLAYER_1

def youwin(pnum, player, p1sc, p2sc):

I assume 'p1sc' means "player_1_score" ?

If so, you may want to use a dict for scores:

   scores = {PLAYER_1:0, PLAYER_2:0}

then...

    if pnum == 1:
        print("CONGRATULATIONS!")

<ot>Is it really necessary to WRITE THIS IN ALL UPPERS ?-)</ot>

    else:
        if player == "P1":
            p1sc += 1
        else:
            p2sc += 1

Which would then become:
          scores[player] +=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

If it's meant to be 0/1 flag, Python has proper booleans too (True and False).

        a, b, target = setup()
    else:
        print("GOOD BYE. PLAY AGAIN SOON!")
        quit()

It might be better to avoid exiting so brutally. Why not returning 0, none, None, None ?

    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!

You should either put this in it's own function (could be named 'main'), or at least "protect" it with an "if __name__ == '__main__':" test.

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.

And it looks way better than your first version.

(snip)
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to