티스토리 뷰

Test/Python(20220101~)

Days12_Guess Number

kiostory 2022. 1. 16. 20:20
def clear_screen():
    import os
    if os.name in ('nt', 'dos'):  # If Machine is running on Windows, use cls
        os.system('cls')
    else:
        os.system('clear')
def guess_what(retry_count):
    import random
    random_number = random.randint(1,100)
    print(random_number)
    for chance in range(retry_count,0,-1): 
        print(f"You have {chance} attempts remaining to guess the number.")
        guess_number = int(input("Make a guess: "))
        if guess_number > random_number:
            print(f"Too high.")
            if  chance > 1:
                print("Guess again.")
        elif guess_number < random_number:
            print(f"Too low.")
            if  chance > 1:
                print("Guess again.")
        else:
            print(f"You got it! The answer was {random_number}.")
            return 0
    print(f"You've run out of guesses, you lose. The number was {random_number}.")
def main():
    clear_screen()
    from title import logo
    print(logo)
    print("""
Welcome to the Numbr Guessing Game!
I'm thinking of a number between 1 and 100.
    """)
    difficulty = input("Choose a difficulty. Type 'easy' or 'hard':").lower()
    if difficulty == 'easy' :
        retry_count = 10
    elif difficulty == 'hard':
        retry_count = 5
    elif difficulty == "":
        retry_count = 5
    else :
        print("Invalid input. Bye.")
        return 0
    guess_what(retry_count)
main()
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함