Advertisement
earlution

Number guessing game

Jun 9th, 2020
986
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.25 KB | None | 0 0
  1. import random
  2.  
  3. def generateRandomNumber():
  4.     return random.randint(1, 100)
  5.  
  6. def askUserForNumber(message = "Guess the number: "):
  7.     userNumber = int(input(message))
  8.     return userNumber
  9.  
  10. def checkUserNumber(userNumber, randomNumber):
  11.     if userNumber > randomNumber:
  12.         return "Too High"
  13.     elif userNumber < randomNumber:
  14.         return "Too Low"
  15.     else:
  16.         return "Congratulations!"
  17.  
  18. def main():
  19.     userCongratulated = False
  20.     letsStart = True
  21.  
  22.     while userCongratulated or letsStart:
  23.         userNumberOfGuesses = 0
  24.         randomNumber = generateRandomNumber()
  25.         # print ("for testing purposes, random number:", randomNumber )
  26.         userNumber = askUserForNumber()
  27.         userNumberOfGuesses += 1
  28.         message = checkUserNumber(userNumber ,randomNumber)
  29.    
  30.         while message != "Congratulations!":
  31.             print(message)
  32.             userNumber = askUserForNumber("Try again ")
  33.             userNumberOfGuesses = userNumberOfGuesses + 1
  34.             message = checkUserNumber( userNumber , randomNumber )
  35.            
  36.             print()
  37.             print(message, "it took you", userNumberOfGuesses,\
  38.                   "goes to guess correctly")
  39.             userCongratulated = True
  40.  
  41. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement