Advertisement
NyteOwlDave

Guess a Number Game

Dec 19th, 2023 (edited)
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. #number guessing game
  2. #welcome the user
  3. #users should input ranges, upper range and lower range
  4. #import random
  5. #create a while loop
  6. #import math
  7.  
  8. import random
  9.  
  10. print "Number guessing game"
  11. name_of_gamer = input("Enter your name:\n")
  12. print "welcome: " + name_of_gamer
  13.  
  14. approval = input("Do you want to play this game?\n")
  15. if approval.lower() == "yes":
  16.     print "Let's go!"
  17. else:
  18.     print "It was nice having you here " + name_of_gamer
  19.     quit()
  20.  
  21. # Enter range limits
  22. upper_range = int(input("Enter any number for your upper range:\n"))
  23. lower_range = int(input("Enter any number for your lower range:\n"))
  24.  
  25. # Swap range limits if required
  26. if upper_range < lower_range:
  27.     upper_range, lower_range = lower_range, upper_range
  28.  
  29. # Show the actual range that was entered
  30. print "Upper limit = " + str(upper_range)
  31. print "Lower limit = " + str(lower_range)
  32.  
  33. # Choose a random number within the range
  34. secret_number = random.randint(lower_range, upper_range)
  35.  
  36. # Show the secret number (for testing)
  37. # print "The secret number = " + str(secret_number)
  38.  
  39. # Get gamer's first guess
  40. gamers_guess = int(input("Enter a guess:\n"))
  41.  
  42. # Guessing loop
  43. while gamers_guess != secret_number:
  44.     # Too high?
  45.     if gamers_guess > secret_number:
  46.         # Get another guess
  47.         print("print your guess is above the number")
  48.         gamers_guess = int(input("Enter another guess:\n"))
  49.     else: # Too low
  50.         # Get another guess
  51.         print("print your guess is below the number")
  52.         gamers_guess = int(input("Enter another guess:\n"))
  53.  
  54. # Cool!
  55. print "We have a winner!"
  56.  
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement