feba wrote:
#!/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.
1. Refactor. You should look at your code and see where you repeat the
same or similar patterns, see where they differ, make functions, and
make the differences parameters to the function call:
def guess(player, p1score, p2score):
guess1 = int(input("\n>> "))
if guess1 > 100:
print("ONLY NUMBERS FROM 1 TO 99")
elif guess1 > target:
print("TOO HIGH")
elif guess1 == target:
print("GOOD JOB, PLAYER %s! THE SCORE IS:" % player)
print("P1: %s --- P2: %s" % (p1score, p2score)))
print("PLAY AGAIN?")
#Set up the game again
play = int(input("0 TO END: "))
if play == 0:
print("GOOD BYE. PLAY AGAIN SOON!")
quit()
else:
target = random.randint(1, 99)
else:
print("TOO LOW")
You would call guess() like this, perhaps:
guess(2, 15, 22)
2. You need to use a try: except: within a loop when you cast the input
to int:
while True:
try:
guess1 = int(input("\n>> "))
except ValueError:
print 'Bad value.'
else:
break
Same with the seeing if the player will play again. See below.
3. Don't try to fit too much on one line. See how I broke the print
statement at logical places (new lines).
4. Further subdivide functions based on conceptual tasks. For example, I
would define a function like this:
def play_again():
while True:
try:
print("PLAY AGAIN?")
again = bool(input("0 TO END: "))
except ValueError:
print 'Bad value.'
else:
break
return again
You would use play_again() like this:
if play_again():
print("GOOD BYE. PLAY AGAIN SOON!")
quit()
else:
target = random.randint(1, 99)
5. Don't do anything until you need to do it. See how I handled setting
the new target for what I mean. You set a new target before you know if
the player wants to play again. Its not a big deal in this case, but
these inefficiencies build up in larger applications. This isn't
"premature optimization", but simply good coding style [ed: put in to
fend off the "premature optimization is bad" puppets who infest this list].
Notice that the sequence of commands follows a logical sequence
corresponding to decisions in the game. It makes the code more sensible,
readable, and a little faster some times to boot. Good logic has the
happy consequence of good speed most of the time.
My code is typed real-time so is untested. No promise that there aren't
typos but you will probably get the idea. But see if you can plug the
these functions into the proper places and apply some of these techniques.
James
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095
http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list