0

This is supposed to be a guessing game. If the user submits a number that is higher than the pre-set value (which is 10), it will say go lower. And if the user submits a lower number, it will say go higher. The problem I'm facing is that it only tells me to go higher with every integer or even string I type in. Any clue what the problem might be?

Also, the reason I turned 10 into a string is because it gives me an error if it's an integer.

from tkinter import *

# Simple guessing game
window = Tk()

def number():
    if typed == key:
        print("This is the correct key")
    elif typed > key:
        print("Go lower!")
    else:
        print("Go higher!")

e = Entry(window)
e.pack()

key = str(10)
typed = e.get()

b = Button(window, text="Submit", command=number)
b.pack()

window.mainloop()
Vitalynx
  • 946
  • 1
  • 8
  • 17
  • 1
    You never update values of `typed` and `key` They are computed once and never changed in any kind of callback. Also, comparing strings will lead you to unexpected behaviour. See [String Comparison Technique Used by Python](http://stackoverflow.com/questions/4806911/string-comparison-technique-used-by-python) for more details. – Łukasz Rogalski Sep 10 '16 at 17:08

1 Answers1

0

You need to update the value on each submit and check or else it will stay constant. The reason why it did not work is because you assigned typed to e.get() on program start, which is an empty string. You need to update it on every click, or put typed inside the function.

You also need to make key an integer, and convert the entry value into an integer, or else you're lexicographically comparing strings. Also, you can add a try/except to handle if the user does not enter an integer, as shown below.

I would personally pass the entry's value into the function and on click use a lambda to process the check:

from tkinter import *

# Simple guessing game
window = Tk()

def number(num): # Passing in string instead of using typed
    try:
        intNum = int(num) # Here we convert num to integer for comparison
        if intNum == key:
            print("This is the correct key")
        elif intNum > key:
            print("Go lower!")
        else:
            print("Go higher!")
    except ValueError: # If user doesn't enter a convertable integer, then print "Not an integer!"
        print("Not an integer!")

e = Entry(window)
e.pack()

key = 10 # Key is an integer here!

b = Button(window, text="Submit", command=lambda: number(e.get())) # Here's a lambda where we pass in e.get() to check
b.pack()

window.mainloop()

So now it will pass the value in, then compare instead of using typed. lambda: number(num) just allows the passing of an argument in the command argument. Here's a link to docs regarding lambdas. Without lambdas, here's a hastebin demonstrating without function arguments.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
  • Hey, I tried this code but it gives "Go lower" when i type in any number between 2 and 9 – Vitalynx Sep 10 '16 at 17:32
  • Yes it works. I'm wondering is there a way of writing this code without using lambda? – Vitalynx Sep 10 '16 at 17:57
  • @user3081954 [Sure there is](http://hastebin.com/upedigiyow.py), just have `e.get()` inside the function. – Andrew Li Sep 10 '16 at 18:01
  • Alright so to sum up: "typed" needs to be defined inside the function otherwise it will have an empty value at program startup. – Vitalynx Sep 10 '16 at 18:10
  • @user3081954 Yes. Inside the function, it will then get the entry value every time when button is clicked, allowing it to compare correctly – Andrew Li Sep 10 '16 at 18:11