Advertisement
earlution

Number guessing game v0.7

Jun 12th, 2020
1,488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. import random
  2.  
  3. def generate_random_number():
  4.     random_number = random.randint(1,100)
  5.     return random_number
  6.  
  7. def capture_guess():
  8.     guess = input("Guess a number between 1 and 100: ")
  9.     return guess
  10.  
  11. def validate_input(guess):
  12.     while True:
  13.         try:
  14.             guess = int(guess)
  15.             while guess <1 or guess > 100:
  16.                 print("The number must be between 1 and 100.")
  17.                 guess = int(input("Guess a number: "))        
  18.             break
  19.         except ValueError:
  20.             print("You must input a integer value!")
  21.             guess = input("Guess a number between 1 and 100: ")
  22.     return guess
  23.  
  24. def evaluate_user_guess(guess, random_number):
  25.     if guess > random_number:
  26.         guess = int(input("lower: "))
  27.     elif guess < random_number:
  28.         guess = int(input("higher: "))
  29.     return guess
  30.  
  31. def guess_random_number(guess, random_number):
  32.     attempts = 1
  33.     while guess != random_number:
  34.         guess = evaluate_user_guess(guess, random_number)
  35.         attempts += 1
  36.     return attempts
  37.  
  38. def final_output(attempts):
  39.     print("Well done!")
  40.     print("You guessed the random number in",  attempts, "attempts.")
  41.  
  42. random_number = generate_random_number()
  43. guess = validate_input(capture_guess())
  44. attempts = guess_random_number(guess, random_number)
  45. final_output(attempts)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement