Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def generate_random_number():
- random_number = random.randint(1,100)
- return random_number
- def capture_guess():
- guess = input("Guess a number between 1 and 100: ")
- return guess
- def validate_input(guess):
- while True:
- try:
- guess = int(guess)
- while guess <1 or guess > 100:
- print("The number must be between 1 and 100.")
- guess = int(input("Guess a number: "))
- break
- except ValueError:
- print("You must input a integer value!")
- guess = input("Guess a number between 1 and 100: ")
- return guess
- def evaluate_user_guess(guess, random_number):
- if guess > random_number:
- guess = int(input("lower: "))
- elif guess < random_number:
- guess = int(input("higher: "))
- return guess
- def guess_random_number(guess, random_number):
- attempts = 1
- while guess != random_number:
- guess = evaluate_user_guess(guess, random_number)
- attempts += 1
- return attempts
- def final_output(attempts):
- print("Well done!")
- print("You guessed the random number in", attempts, "attempts.")
- random_number = generate_random_number()
- guess = validate_input(capture_guess())
- attempts = guess_random_number(guess, random_number)
- final_output(attempts)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement