Python Project – Guess Number

Python Project – Guess Number

import random
import tkinter as tk

# Generate a random number between 1 and 100
number = random.randint(1, 100)

# Create the main window
root = tk.Tk()
root.title(“Guess the Number”)

# Create a label to display instructions
label = tk.Label(root, text=”Guess a number between 1 and 100:”)

# Create an entry box for the player’s guess
guess_box = tk.Entry(root)

# Keep track of the number of guesses
num_guesses = 0

# Define a function to check the player’s guess
def check_guess():
    global num_guesses
    guess = int(guess_box.get())
    guess_box.delete(0, tk.END)
    num_guesses += 1
    if guess == number:
        feedback_label.config(text=”Congratulations, you guessed the number in ” + str(num_guesses) + ” guesses!”)
    elif guess < number:
        feedback_label.config(text=”Too low! Guess again.”)
        feedback_label.config(text=”Too high! Guess again.”)

# Create a button to submit the guess
guess_button = tk.Button(root, text=”Guess”, command=check_guess)

# Create a label to display feedback
feedback_label = tk.Label(root, text=””)

# Start the main loop

Leave a Reply

Your email address will not be published. Required fields are marked *