Skip to content

Commit 361c1a1

Browse files
committed
Adding guessing game with tkinter code and readme
1 parent cd99401 commit 361c1a1

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

Guessing_Game_GUI/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Guessing Game GUI
2+
_A random number is generated between an upper and lower limit both entered by the user. The user has to enter a number and the algorithm checks if the number matches or not within three attempts._
3+
4+
## Modules used
5+
6+
- tkinter
7+
- random
8+
9+
## How it works
10+
11+
- User enters the upper limit and lower limit number (example 7 and 10) and locks these numbers.
12+
- When the user locks these numbers a random number in the given range is generated by the random module.
13+
- User guesses a number and enters it.
14+
- The GUI displays if the number is correct or wrong within three attempts.
15+
- After three attempts the user can either *play again* or *quit*
16+
17+
Made by [K. Sai Drishya](https://github.com/saidrishya)
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# -*- coding: utf-8 -*-
2+
"""guessing_game_tkinter.ipynb
3+
4+
Automatically generated by Colaboratory.
5+
6+
Original file is located at
7+
https://colab.research.google.com/drive/1OzOm77jUVrxo1jWWlQoXkj19Pc8D_GdC
8+
"""
9+
10+
from tkinter import *
11+
import random
12+
13+
root = Tk()
14+
root.title('Number Guessing Game')
15+
root.geometry('450x450+50+50')
16+
17+
font_used = ('Arial', 12)
18+
#lower limit number
19+
num1 = IntVar()
20+
#upper limit number
21+
num2 = IntVar()
22+
#user guessed value
23+
guessed_num = IntVar()
24+
#value is the random value generated
25+
global value
26+
value= None
27+
global attempts
28+
attempts = 3
29+
30+
#generate random number
31+
def get_rand():
32+
b4.configure(state=DISABLED)
33+
global value
34+
value = random.randint(num1.get(), num2.get())
35+
36+
37+
#resets the window
38+
def reset():
39+
num1.set(0)
40+
num2.set(0)
41+
guessed_num.set(0)
42+
global attempts
43+
attempts = 3
44+
b4.configure(state=NORMAL)
45+
b1.configure(state = NORMAL)
46+
l4.configure(text = '')
47+
l5.configure(text = 'You have 3 attempts left')
48+
b2.configure(state = DISABLED)
49+
50+
51+
def check():
52+
global value
53+
#Label(root, text=str(value)).grid(row=8)
54+
55+
#tells the user how many attempts are left
56+
global attempts
57+
attempts -= 1
58+
59+
#if all attempts are over
60+
if attempts == 0 and guessed_num.get() != value:
61+
b1.configure(state=DISABLED)
62+
l5.configure(text = 'You have 0 attempts left')
63+
l4.configure(text='Sorry! All attempts done. The correct answer is ' + str(value), fg='red')
64+
b2.configure(text = 'Play Again', command=reset, state=NORMAL)
65+
b2.grid(row=9, column=1)
66+
b3.configure(text = 'Quit', command = root.quit)
67+
b3.grid(row = 9, column=2)
68+
else:
69+
#if attempts are still left
70+
71+
#if guessed value is correct
72+
if guessed_num.get() == value:
73+
l4.configure(text='Congratulations! You are right')
74+
b2.configure(text = 'Play Again', command=reset, state =NORMAL)
75+
b2.grid(row=9, column=1)
76+
b3.configure(text = 'Quit', command = root.quit)
77+
b3.grid(row = 9, column=2)
78+
79+
#if guessed value is incorrect
80+
else:
81+
if guessed_num.get() > value:
82+
l4.configure(text='Better Luck Next time! Try a lesser number')
83+
else:
84+
l4.configure(text='Better Luck Next time! Try a greater number')
85+
l5.configure(text = 'You have ' + str(attempts) + ' attempts left')
86+
87+
l6 = Label(root, text = 'Input fields cannot be 0', font=font_used, fg='red')
88+
l6.grid(row=0, columnspan=3, pady=5)
89+
90+
# from i.e lower limit
91+
l1 = Label(root, text = 'From', font=font_used, fg='red')
92+
l1.grid(row=1, column=0, sticky=W)
93+
e1 = Entry(root, textvariable = num1, font=font_used, fg='blue')
94+
e1.grid(row=1, column=1, padx=5, pady=5)
95+
96+
# to i.e upper limit
97+
l2 = Label(root, text = 'To', font=font_used, fg='red')
98+
l2.grid(row=2, column=0, sticky=W, padx=5, pady=5)
99+
#locking numbers is neccessary as it generates only a single random number for the limit
100+
b4 = Button(root, text = 'Lock Numbers', fg='magenta', command=get_rand)
101+
b4.grid(row=3, columnspan=3, pady=5)
102+
e2 = Entry(root, textvariable = num2, font=font_used, fg='blue')
103+
e2.grid(row=2, column=1, padx=5, pady=5)
104+
105+
106+
#guess the number
107+
l3 = Label(root, text = 'Guess any number', font=font_used, fg='darkviolet')
108+
l3.grid(row=4, columnspan=3,pady=5)
109+
110+
#label for showing the number of attempts
111+
l5 = Label(root, text = 'You have 3 attempts left', font=font_used, fg='darkviolet')
112+
l5.grid(row=5, columnspan=3,pady=5)
113+
114+
#the user enters the guessed number
115+
e3 = Entry(root, textvariable = guessed_num, font=font_used, fg='darkorchid')
116+
e3.grid(row=6, columnspan=3,pady=5)
117+
118+
#checks whether the guessed number is correct or not
119+
b1 = Button(root, text='Check', font=font_used, fg='darkviolet', command=check)
120+
b1.grid(row=7, columnspan=3, pady=5)
121+
122+
#displays the result
123+
l4 = Label(root, text = 'Result', font=font_used, fg='magenta')
124+
l4.grid(row=8, columnspan=3,pady=5)
125+
126+
#button for play again
127+
b2 = Button(root)
128+
#button for quit which closes the window
129+
b3 = Button(root)
130+
131+
132+
root.mainloop()
133+

0 commit comments

Comments
 (0)