import random import tkinter as tk from tkinter import messagebox # Create the main window root = tk.Tk() root.title("Number Guessing Game") # Variables number_to_guess = random.randrange(100) chances = 7 guess_counter = 0 # Function to check the guess def check_guess(): global guess_counter, chances, number_to_guess try: my_guess = int(guess_entry.get()) # Get the number from the entry box except ValueError: messagebox.showerror("Invalid Input", "Please enter a valid number") return guess_counter += 1 if my_guess == number_to_guess: messagebox.showinfo("Congratulations", f'The number is and you found it right in attempts!') reset_game() elif guess_counter >= chances: messagebox.showinfo("Game Over", f'Oops, the number was. Better luck next time!') reset_game() elif my_guess > number_to_guess: result_label.config(text="Your guess is too high!") elif my_guess < number_to_guess: result_label.config(text="Your guess is too low!") # Function to reset the game def reset_game(): global guess_counter, number_to_guess guess_counter = 0 number_to_guess = random.randrange(100) guess_entry.delete(0, tk.END) # Clear the entry box result_label.config(text="You have 7 chances to guess the number.") # UI Setup title_label = tk.Label(root, text="Welcome to the Number Guessing Game!", font=("Arial", 14)) title_label.pack(pady=10) instructions_label = tk.Label(root, text="You have 7 chances to guess a number between 0 and 99.") instructions_label.pack(pady=5) guess_label = tk.Label(root, text="Enter your guess:").
guess_label.pack() guess_entry = tk.Entry(root, width=10) guess_entry.pack(pady=5) submit_button = tk.Button(root, text="Submit Guess", command=check_guess) submit_button.pack(pady=10) result_label = tk.Label(root, text="Good luck!", font=("Arial", 12)) result_label.pack(pady=10) # Start the GUI event loop root.mainloop() Powered by TCPDF (www.tcpdf.org).