titanium_savant

Untitled

Oct 20th, 2021
758
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from ascii_art import day12_logo as logo
  2. import random
  3. import sys
  4.  
  5. def get_user_input(prompt, valid_response, error_msg):
  6.     """prompt the user for a response until a valid response is given"""
  7.     user_input = input(prompt)
  8.     while user_input not in valid_response:
  9.         print(error_msg)
  10.         user_input = input(prompt)
  11.     return user_input
  12.  
  13. def eval_guess(guess):
  14.     if guess == my_number:
  15.         print(f'You got it! The answer was {my_number}.')
  16.         return True
  17.     elif guess > my_number:
  18.         print('Too high.\nGuess again.')
  19.     elif guess < my_number:
  20.         print('Too low.\nGuess again.')
  21.     else:
  22.         print(f'no idea what happend. guess: {guess}  number {my_number}')
  23.         sys.exit()
  24.     return False
  25.  
  26.  
  27. my_number = random.randint(1,100)
  28.  
  29. print(logo)
  30. difficulty = get_user_input("Choose a difficulty. Type 'easy' or 'hard': ", ['easy', 'hard'], "I didn't get that. try again")
  31. if difficulty == 'easy':
  32.     attempts = 10
  33. else:
  34.     attempts = 5
  35.  
  36.  
  37. for attempt in range(attempts):
  38.     print(f"You have {attempts - attempt} attempts remaining to guess the number.")
  39.     guess = int(input("Make a guess: "))
  40.     if eval_guess(guess):
  41.         break
  42. else:
  43.     print("You've run out of guesses, you lose.")
RAW Paste Data