This repository was archived by the owner on Dec 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguessing_game_v2.py
More file actions
65 lines (56 loc) · 2.14 KB
/
guessing_game_v2.py
File metadata and controls
65 lines (56 loc) · 2.14 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
__author__ = 'Andrew Edwards'
from tkinter import *
import tkinter.messagebox
from random import *
def exit_program():
confirm_exit = tkinter.messagebox.askquestion("Exit", "Are you sure you want to exit?")
if confirm_exit == "yes":
sys.exit()
class Application:
def __init__(self, master):
# Build the frame of the game
frame = Frame(master)
frame.pack()
# Build the top menu
top_menu = Menu(root)
root.config(menu=top_menu)
# Build the sub menu
sub_menu = Menu(top_menu)
top_menu.add_cascade(label="File", menu=sub_menu)
sub_menu.add_command(label="Exit", command=exit_program)
# Give some instructions
self.instruct = Label(root, text="""
I'm thinking of a number
between 1 and 999.
Take a guess!
""")
self.instruct.pack()
# Ask for user input
user_guess = Entry(root, width=3)
user_guess.pack()
user_guess.insert(0, 0) # Ensures that user_guess returns an integer before user enters anything
user_number = int(user_guess.get()) # Pulls the user input from the Entry widget, assigns it to a variable.
submit_button = Button(text="Submit", command=lambda: self.button_event(user_number))
submit_button.pack()
def button_event(self, user_input):
# Variables
random_number = randint(1, 999)
player_guess = user_input
number_guesses = 0
# Main game logic loop
while True:
number_guesses += 1
if player_guess == random_number:
tkinter.messagebox.askokcancel("You Win!", "You got it in " + str(number_guesses) + " tries!")
break
elif player_guess > random_number:
tkinter.messagebox.askokcancel("Nope", "You are too high!")
break
else:
tkinter.messagebox.askokcancel("Nope", "You are too low!")
break
if number_guesses > 10:
tkinter.messagebox.OK("You Lose!", "Sorry, the number I was thinking of was " + str(random_number))
root = Tk()
a = Application(root)
root.mainloop()