Need help improving number guessing game

feba febaen at gmail.com
Sat Dec 13 03:57:12 EST 2008


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

import random

print(" --- WELCOME TO THE SUPER NUMBER GUESSING GAME --- " + ("\n" *
5))
pnum = int(input("1 OR 2 PLAYER?\nP#: "))

target = random.randint(1, 99) #Pick a random number under two digits

guess1 = 0 #Zero will never be picked as target...
guess2 = 0 #so it makes a good default value
p1score = 0 #For two player mode...
p2score = 0 #let's keep score!

print("LET'S START THE GAME. \nGUESS ANY WHOLE NUMBER FROM 1 TO 99.")

while True:
        if pnum == 1: #1p mode
            while True:
                guess1 = int(input("\n>> "))
                if guess1 > 100:
                    print("ONLY NUMBERS FROM 1 TO 99")
                elif guess1 > target:
                    print("TOO HIGH")
                elif guess1 == target:
                    print("CONGLATGURATIONS! PLAY AGAIN?")
                    target = random.randint(1, 99) #Set up the game
again
                    play = int(input("0 TO END: "))
                    if play == 0:
                        print("GOOD BYE. PLAY AGAIN SOON!")
                        quit()
                else:
                    print("TOO LOW")

        if pnum == 2: #2p mode
            while True:
                guess1 = int(input("\nP1> ")) #Player 1's turn
                if guess1 > 100:
                    print("ONLY NUMBERS FROM 1 to 99")
                elif guess1 > target:
                    print("TOO HIGH")
                elif guess1 == target:
                    p1score += 1
                    print("GOOD JOB, PLAYER 1! THE SCORE IS:\nP1: %s
--- P2: %s\nPLAY AGAIN?" % (p1score, p2score))
                    target = random.randint(1, 99) #Set up game
again
                    play = int(input("0 TO END: "))
                    if play == 0:
                        print("GOOD BYE. PLAY AGAIN SOON!")
                else:
                    print("TOO LOW")

                guess2 = int(input("\nP2> ")) #Player 2's turn
                if guess2 > 100:
                    print("ONLY NUMBERS FROM 1 to 99")
                elif guess2 > target:
                    print("TOO HIGH")
                elif guess2 == target:
                    p2score += 1
                    print("GOOD JOB, PLAYER 2! THE SCORE IS:\nP1: %s
--- P2: %s\nPLAY AGAIN?" % (p1score, p2score))
                    target = random.randint(1, 99) #Set up game again
                    play = int(input("0 TO END: "))
                    if play == 0:
                        print("GOOD BYE. PLAY AGAIN SOON!")
                else:
                    print("TOO LOW")
        else:
            print("INVALID PLAYER SELECTION")
            pnum = int(input("1 OR 2 PLAYER?\nPN#: "))


I have one major problem with this; the 'replay' selection. It quits
if you put in 0, as it should, and continues if you put in any other
number. However, if you just press enter, it exits with an error. it
also looks really ugly, and I'm sure there has to be plenty of better
ways to do it.

I'd also appreciate tips on how it could be better in general. I
should think that P1 and P2's turns shouldn't have to be completely
repeated; but I'm not quite sure how to def something like that.



More information about the Python-list mailing list