I think my code considers one of my variables higher than the other but can't seem to figure out why?

Question:

Basically, I have this code where you choose a number (stored in a var called "chn") and then the program compares it to another randomly chosen number (stored in a var called "rng") and the progtam shows all that on a GUI. It then tells the user if ‘chn’ is higher or lower than ‘rng’ but for some reasons if I write :

    if chn < rng:
        label2['text'] = 'Too low' 
    elif chn > rng:
        label2['text'] = 'Too high'
    else:
        label2['text'] = 'You won !'

it will print ‘Too low’ no matter the value.

but if I write :

    if chn > rng:
        label2['text'] = 'Too low' 
    elif chn < rng:
        label2['text'] = 'Too high'
    else:
        label2['text'] = 'You won !' 

it will print ‘Too high’ no matter the value.

Here is the full code

from random import *
from tkinter import *

rng = str(randint(1, 100))


mf = Tk()
mf.title('Deviner le nombre')
mf.geometry('480x400')
mf.resizable(width=True, height=True)
mf['bg'] = 'white'


label1 = Label(mf, text='Deviner le bon numéro !', fg='Black', bg='white')
label1['font'] = ('Arial', 20)
label1.place(x=85, y=50)

bouton1 = Button(mf, text='Quitter', width=10, height=2, command=mf.destroy)
bouton1.place(x=350, y=300)

chn = str()


entryChn = Entry(mf, textvariable=chn)
entryChn.place(x=180, y=100)


label2 = Label(mf, text='', fg='Red', bg='white')
label2['font'] = ('Arial', 16)
label2.place(x=65, y=150)

chn = str(chn)


def verifier():
    if chn < rng:
        label2['text'] = 'Too low'
    elif chn > rng:
        label2['text'] = 'Too high'
    else:
        label2['text'] = 'You won !'


def effacer():
    label2['text'] = ''


bouton2 = Button(mf, text='Vérifier', width=10, height=2, command=verifier)
bouton2.place(x=80, y=300)

bouton3 = Button(mf, text='Relancer', width=10, height=2, command=effacer)
bouton3.place(x=215, y=300)

mf.mainloop()

I tried switching the symbols but it didn’t work. I tried assigning defined values to ‘chn’ and ‘rng’ but it always gives me an output where ‘rng’ > ‘chn’, apart from that everything works well in the code.
I don’t know if I’m just oblivious but I’ve been trying to figure this out for a few days now, please help lol.

Asked By: Youxi

||

Answers:

Here chn should be a StringVar() instead of str().
Also, to get value from the Entry, it is required to use chn.get().

The content of chn is string, string comparison compares strings in lexicographic order, which doesn’t work as expected in here. So, it have to be explicitly converted to int before comparison.

The code after those changes:

from random import *
from tkinter import *

rng = str(randint(1, 100))


mf = Tk()
mf.title('Deviner le nombre')
mf.geometry('480x400')
mf.resizable(width=True, height=True)
mf['bg'] = 'white'


label1 = Label(mf, text='Deviner le bon numéro !', fg='Black', bg='white')
label1['font'] = ('Arial', 20)
label1.place(x=85, y=50)

bouton1 = Button(mf, text='Quitter', width=10, height=2, command=mf.destroy)
bouton1.place(x=350, y=300)

chn = StringVar()


entryChn = Entry(mf, textvariable=chn)
entryChn.place(x=180, y=100)


label2 = Label(mf, text='', fg='Red', bg='white')
label2['font'] = ('Arial', 16)
label2.place(x=65, y=150)


def verifier():
    if int(chn.get()) < int(rng):
        label2['text'] = 'Too low'
    elif int(chn.get()) > int(rng):
        label2['text'] = 'Too high'
    else:
        label2['text'] = 'You won !'


def effacer():
    label2['text'] = ''


bouton2 = Button(mf, text='Vérifier', width=10, height=2, command=verifier)
bouton2.place(x=80, y=300)

bouton3 = Button(mf, text='Relancer', width=10, height=2, command=effacer)
bouton3.place(x=215, y=300)

mf.mainloop()
Answered By: Mislah
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.