Reputation: 19
I am trying to make a guess game program where you have to guess a number between 0-100 on a GUI using Tkinter
, and it also counts your amount of attempts but I get this error on line 25:
'<' is not supported between instances of str and int.
What can I do to solve this issue? This code would work on a command line but not when I have attempted to translate it into a GUI. I am also not sure if the code even works.
My updated code so far is:
import random
from tkinter import *
#need to count how many attempts you made
window = Tk()
window.title("Number guessing game")
window.geometry('350x200')
lbl = Label(window, text="Enter a number here from 1-100: ")
lbl.grid(column=0,row=0)
guess_var = IntVar(window)
txt = Entry(window, textvariable=guess_var)
txt= Entry(window, width=10)
txt.grid(column=1,row=0)
numguess = 0
secret_num = random.randint(1, 100)
def clicked():
if guess < 0:
lbl2.configure(text ="Please enter a sensible number.")
if guess > 100:
lbl2.configure(text ="Please enter a sensible number.")
if guess == secret_num:
lbl2.configure(text ="You guessed correctly.")
lbl3.confgure(text=numguess)
elif guess < secret_num:
numguess = numguess + 1
lbl2.configure(text ="higher!")
lbl = Label(window, text="Enter a number here from 1-100: ")
else:
numguess = numguess + 1
lbl2.configure(text ="lower!")
lbl = Label(window, text="Enter a number here form 1-100")
lbl2 = Label(window,text = " ")
lbl2.grid(column=1, row=2)
lbl3 = Label(window,text = " ")
lbl3.grid(column=1, row=3)
btn = Button(window, text="Enter", command=clicked)
guess = int(txt.get())
btn.grid(column=3, row=0)
window.mainloop()
Upvotes: 0
Views: 921
Reputation: 4583
is not supported between instances of str and int.
There are sufficient errors.
Don't use wildcard from tkinter import *
for Python 3.x
global numguess
guess
to int(txt.get())
lbl3.confgure
to
lbl3.configure
tk.Label
widget outside of function
and add lbl.grid(column=0, row=0)
too.configure
to lbl.configure(text=f"Enter a number here form 1-100")
#guess = int(txt.get())
. So you don't need
this.You're ready to got.
Snippet modified to reduced code:
import random
import tkinter as tk
#need to count how many attempts you made
window = tk.Tk()
window.title("Number guessing game")
window.geometry('350x200')
lbl = tk.Label(window, text="Enter a number here from 1-100: ")
lbl.grid(column=0,row=0)
guess_var = tk.IntVar(window)
txt = tk.Entry(window, textvariable=guess_var, bg='red')
txt= tk.Entry(window, width=10)
txt.grid(column=1,row=0)
numguess = 0
secret_num = random.randint(1, 100)
def clicked():
global numguess
#if guess < 0:
#lbl2.configure(text ="Please enter a sensible number.")
#if guess > 100:
#lbl2.configure(text ="Please enter a sensible number.")
if int(txt.get()) == secret_num:
lbl2.configure(text =f"You guessed correctly.")
lbl3.configure(text=numguess)
elif int(txt.get()) < secret_num:
numguess = numguess + 1
lbl2.configure(text =f"higher!")
lbl.configure(text=f"Enter a number here form 1-100")
else:
numguess = numguess + 1
lbl2.configure(text =f"lower!")
lbl.configure(text=f"Enter a number here form 1-100")
lbl = tk.Label(window, text=f"Enter a number here form 1-100")
lbl.grid(column=0, row=0)
lbl2 = tk.Label(window)
lbl2.grid(column=1, row=2)
lbl3 = tk.Label(window)
lbl3.grid(column=1, row=3)
btn = tk.Button(window, text="Enter", command=clicked)
#guess = int(txt.get())
btn.grid(column=3, row=0)
window.mainloop()
Screenshot:
Upvotes: 0
Reputation: 4481
guess = txt.get()
returns a string value. To be able to perform comparisons with integers, you need to convert guess
from a string to an integer, i.e.:
guess = int(txt.get())
To be safe, you may want to handle the case where the user inputs something which cannot be converted to a integer; how to do this depends on the design of your problem - you should consider validating the user input.
One way to do this could be by using IntVar
, e.g.:
guess_var = IntVar(window)
txt = Entry(window, textvariable=guess_var)
txt.grid(column=1,row=0)
numguess = 0
secret_num = random.randint(1, 100)
guess = guess_var.get()
Note that if you want to use the numguess
global variable inside your clicked
function, you need to declare global numguess
in your function definition, e.g.:
def clicked():
global numguess
Upvotes: 2