I am working on another program, this time it's a guessing game.
I have spend so many hours late in the night, but i can't figure out how to fix this problem.
I won't the program to read the input number and display if it is bigger, smaller or if you got it.
If i don't tipe enything the if statement works, but if I tipe a number it allways says that is smaller and the counting also doesn't work.
I am still learning the bases, so I would appreciated your help very much.

from Tkinter import*

import random
number = random.randint(1,100)
print number #
labelInputNumber=Label(tk,text="Number: ")
labelInputNumber.grid(row=0, column=0)
tries = 0
def number():
    tries = 0
    Input = caseInputNumber.get()
    print Input #
    if Input =="":
        warningLabel=Label(tk, text="You didn't input enithing")  #This works
        warningLabel.grid(row=2, column=0)
        tries +=1
    elif Input < number:
        warningLabel=Label(tk, text="The number is bigger!")
        warningLabel.grid(row=2, column=0)
        tries +=1
    elif Input > number:
        warningLabel=Label(tk, text="The number is smaller!")
        warningLabel.grid(row=2, column=0)
        tries +=1
    print tries  #
buton_try=Button(tk,text = "Guess",command=number)
8 Years
Discussion Span
Last Post by zrin

You are trying to compare integer number with string Input.

Also, don't create a new label for the warning each time a guess is made. Create the label once and then change its text.

Edited by vegaseat: label


So the problem is that the Input is treated as a string.
I tried to change

Input = int(caseInputNumber.get())

, but now is constantly telling me that the number is too big.
I don't know how else I would tell python that Input is a number.
Am I going in the right direction or I need to do something else, or I soud change approach?

>>> Input = '55'
>>> type(Input)
<type 'str'>
>>> n = int(Input)
>>> n
>>> type(n)
<type 'int'>

Edited by snippsat: n/a


There are a number of problems in your code.

1) you are using number for a variable and a function

2) int() will not work with an empty string, so put 0 (a zero) into Entry

3) give attention to your coding order, imports first and function defines next then the main program code

Here is your code modified to at least work ...

from Tkinter import*
import random

def check_number():
    global number, tries
    myinput = int(caseInputNumber.get())
    #myinput = int(v.get())
    print number, "-->", myinput # test
    if myinput == 0:
        warningLabel["text"] = "You didn't input anything"
        tries +=1
    elif myinput < number:
        warningLabel["text"] = "The number is bigger!"
        tries +=1
    elif myinput > number:
        warningLabel["text"] = "The number is smaller!"
        tries +=1
    print tries  # test

tk = Tk()

# use this to set a value in Entry()
v = StringVar()

number = random.randint(1,100)
#print number # test
tries = 0

labelInputNumber = Label(tk, text="Number: ")
labelInputNumber.grid(row=0, column=0)

caseInputNumber = Entry(tk, textvariable=v )
caseInputNumber.grid(row=0, column=1)

buton_try = Button(tk, text="Guess", command=check_number)
buton_try.grid(row=0, column=2)

warningLabel = Label(tk, text="-------------")
warningLabel.grid(row=1, column=0)


Now you have to do something if the number is guessed right.


Thank you all very much. I am almost done now but I have 2 litle problems that I can't figure out.
First I wont to make a reset button and I have no idea how to do it.
Second, I have created a listbox where I wont to display all the incorect inputs but it doesn't let me insert int() objects in the list.
That is my try.
I have add this to the code.

if myinput != number:              
        for i in myinput:

box = Listbox(tk, height = 20)

How can I insert int() objects in the list?
I forgot, the reset button has to reset all, exept for one label that cuntes victories.

Edited by zrin: n/a


You have to create the box first before you can use it. Also, convert the integer number to a string with function str() before you add it to the box.


I forgot, the reset button has to reset all, exept for one label that cuntes victories.

You can delete all items from the listbox and then re-insert the victory counter.
listbox.delete(0, END) ## deletes all items
listbox.insert(END, newitem)


I know that I am becoming annoying with this questions, but I am working from 5 days on this and I was getting really flustrated, but now tnx to you I am almost done.
I converted integer number to a string with str() and it works, the only problem now is that the resoults in the listbox. Every simbol is getting in its own row I can't figure out why.

if myinput != number:           
        if myinput < number:
            a = myinput, "< X"
            for i in str(a):
        elif myinput > number:
            b =  myinput ,"> X"
            for i in str(b):

I have also delited all items in the listbox, the only problem that I have to solve now it's how to reset the random number.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.