评价一下发癫的python猜数字

94 阅读1分钟

本菜早上想复习一下循环和选择控制,就产出了下面的东西,令人苦笑不得。

import random

def check():
    # Ask if the user wants to try again
    while True:
        check = input("Once again? (y or n): ")
        if check.lower() == "y": # Convert to lowercase for easier comparison
            return 1
        elif check.lower() == "n":
            return 0
            print("Successfully exited!")
        else:
            print("Invalid input! Please enter 'y' or 'n'.")

def numberGuess():
    num = random.randint(1,20)
    while True:
        guess = input("Please enter the number u guess: ")
        try:
            guess = int(guess)
        except ValueError:
            print("Invalid input! Please enter a number.")
            continue  # Skip the rest of the loop and prompt again
        if guess == num:
            print("You got it!")
            break
        elif guess > num:
            print("To high!")
            if check():
                continue
            else:
                break
        else:
            print("To low!")
            if check():
                continue
            else:
                break

numberGuess()

试了一下,确实能爬。