James Stroud wrote:
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")

I realized this has a bug. The target is locked in the scope of the function. I wouldn't use global, though:

def guess(player, p1score, p2score):
  target = None
  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")

Use it like this:

new_target = gues(player, p1score, p2score)
if new_target is not None:
  target = new_target

I officially declare that I can't guarantee no more bugs in my previous post. I just fixed this one because my conscience was bothering me.

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

Reply via email to