python3 猜数字小游戏

Guess_the_Number.py

import random                                                                                                                                          
                                                                                                                                                       
# Generate a random number between 1 and 100                                                                                                           
number = random.randint(1, 100)                                                                                                                        
                                                                                                                                                       
# Set the initial number of guesses to 0                                                                                                               
guesses = 0                                                                                                                                            
                                                                                                                                                       
while True:                                                                                                                                            
    # Ask the player for a guess                                                                                                                       
    guess = int(input("Guess the number between 1 and 100: "))                                                                                         
                                                                                                                                                       
    # Increment the number of guesses                                                                                                                  
    guesses += 1                                                                                                                                       
                                                                                                                                                       
    # Check if the guess is correct                                                                                                                    
    if guess == number:                                                                                                                                
        print("Congratulations! You guessed the number in", guesses, "guesses.")                                                                       
        break                                                                                                                                          
    elif guess < number:                                                                                                                               
        print("Too low! Try again.")                                                                                                                   
    else:                                                                                                                                              
        print("Too high! Try again.")
posted @ 2023-04-20 09:38  武平宁  阅读(42)  评论(0)    收藏  举报