Advertisement
Fizzik

Untitled

Dec 21st, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. #!/usr/bin/env/python -tt
  2. #Created by Troy
  3. """
  4. This game prompts for the maximum range in which a number
  5. is generated. The player then has to guess the number generated
  6. in this range. Feedback is supplied to the player.
  7.  
  8. Things to add:
  9. 1. Game counter
  10. 2. Try counter
  11. """
  12.  
  13. from sys import exit
  14. import random
  15.  
  16. def again():
  17.     again = '0'
  18.     while again != 'y' and again != 'Y' and again != 'n' and again != 'N':
  19.         again = raw_input("Would you like to play again 'Y/N': ")
  20.         if again == 'y' and again 'Y':
  21.             main()
  22.         elif again == 'n' and again 'N':
  23.             sys.exit(0)
  24.  
  25. #Prompts the player for a guess.
  26. def guess(r):
  27.     ans =  int(raw_input("Enter your guess:\n> "))
  28.     while ans != r:
  29.         if ans < r:
  30.             ans = int(raw_input("You're too low. Try again:\n> "))
  31.         elif ans > r:
  32.             ans = int(raw_input("You're too high. Try again:\n> "))
  33.         else:
  34.             ans = int(raw_input("Make sure you're entering an integer. Try again:\n> "))
  35.     else:
  36.         print "You win!\n"
  37.         again()
  38.                
  39. #Generates the random integer.
  40. def intgen(s):
  41.     chosen = random.randint(1, s)
  42.     guess(chosen)
  43.  
  44. #This prompts and invokes the random integer generator function.
  45. def main():
  46.     print "Welcome to the Number Guessing Game!\n"
  47.     max = int(raw_input("Pick a range maximum. Must be an integer\n> "))
  48.     intgen(max)
  49.  
  50. if __name__ == "__main__":
  51.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement