|
| 1 | +import random |
| 2 | + |
| 3 | +logos= ''' |
| 4 | + _____ _ _ _ |
| 5 | + / ____| | | | | | | |
| 6 | + | | __ _ _ ___ ___ ___ | |_| |__ ___ _ __ _ _ _ __ ___ | |__ ___ _ __ |
| 7 | + | | |_ | | | |/ _ \/ __/ __| | __| '_ \ / _ \ | '_ \| | | | '_ ` _ \| '_ \ / _ \ '__| |
| 8 | + | |__| | |_| | __/\__ \__ \ | |_| | | | __/ | | | | |_| | | | | | | |_) | __/ | |
| 9 | + \_____|\__,_|\___||___/___/ \__|_| |_|\___| |_| |_|\__,_|_| |_| |_|_.__/ \___|_| |
| 10 | +''' |
| 11 | + |
| 12 | +EASY_LEVEL_ATTEMPTS = 10 |
| 13 | +HARD_LEVEL_ATTEMPTS = 5 |
| 14 | + |
| 15 | +def set_difficulty(level): |
| 16 | + if level == 'easy': |
| 17 | + return EASY_LEVEL_ATTEMPTS |
| 18 | + elif level == 'hard': |
| 19 | + return HARD_LEVEL_ATTEMPTS |
| 20 | + else: |
| 21 | + return |
| 22 | + |
| 23 | +def check_answer(guessed_num,answer,attempts): |
| 24 | + if guessed_num < answer: |
| 25 | + print("Your guess is too low") |
| 26 | + return attempts-1 |
| 27 | + elif guessed_num > answer: |
| 28 | + print("Your guess is too high") |
| 29 | + return attempts-1 |
| 30 | + else: |
| 31 | + print(f"Your guess is right... The answer was {answer}") |
| 32 | + |
| 33 | +def guess_number(): |
| 34 | + print(logos) |
| 35 | + print("Let me think of a number between 1 to 50: ") |
| 36 | + answer = random.randint(1, 50) |
| 37 | + level = input("Choose a difficulty. Type 'easy' or 'hard': ") |
| 38 | + attempts = set_difficulty(level) |
| 39 | + if attempts!=EASY_LEVEL_ATTEMPTS and attempts!=HARD_LEVEL_ATTEMPTS: |
| 40 | + print("You Have entered invalid input.\nPlease choose from the above options: \n1.easy\n2.hard") |
| 41 | + return |
| 42 | + guessed_num = 0 |
| 43 | + while guessed_num != answer: |
| 44 | + print(f"You have {attempts} remaining to guess the number.") |
| 45 | + guessed_num = int(input("Guess the number: ")) |
| 46 | + attempts = check_answer(guessed_num, answer, attempts) |
| 47 | + if attempts == 0: |
| 48 | + print("You are out of guesses...you lose!") |
| 49 | + return |
| 50 | + elif guessed_num!=answer: |
| 51 | + print("Guess again.") |
| 52 | +guess_number() |
| 53 | + |
0 commit comments