Tom_the_cat
Tom_the_cat

Reputation: 169

TypeError: unorderable types: int() > Guessing_game()

I am studying python and trying to make a guessing number program that is connected with GUI. However, there is bug and I don't know how to fix, so please help me.

My code is

from tkinter import*
import random

class Application(Frame):

    def __init__(self, master):
        super(Application, self).__init__(master)
        self.grid()
        self.widgets()
        self.answer = Guessing_game(starting_number = 0,
                                    ending_number = 100)

    def widgets(self):

        Label(self,
              text = "Hello welcome to new_version of the Guess My Number!"
              ).grid(row = 0, column = 0, sticky = W)

        Label(self,
              text = "Guess the number(0-100):"
              ).grid(row = 1, column = 0, sticky = W)

        self.user_answer = Entry(self)
        self.user_answer.grid(row = 1, column = 1, sticky = W)

        Button(self,
               text = "submit",
               command = self.submit
               ).grid(row = 3, column = 0, sticky = W)

        self.txt = Text(self, width = 50, height = 20, wrap = WORD)
        self.txt.grid(row = 4, column = 0, columnspan = 4, sticky = W)

    def submit(self):

        user_answer = self.user_answer.get()    

        if user_answer != None:
           int(user_answer) 
        if int(user_answer) not in range(101):
            self.txt.delete(0.0, END)
            self.txt.insert(0.0, "Your guess is not in proper range")
        elif int(user_answer) > self.answer:
            self.txt.delete(0.0, END)
            self.txt.insert(0.0, "Your guess is higher than the answer")
        elif int(user_answer) < self.answer:
            self.txt.delete(0.0, END)
            self.txt.insert(0.0, "Your guess is lower than the answer")
        else:
            self.txt.delete(0.0, END)
            self.txt.insert(0.0, "Your guess is right! the number is", self.answer)

class Guessing_game(object):
    def __init__(self, starting_number, ending_number):
        self.answer = random.randint(starting_number,ending_number)

    def __str__(self):
        return self.answer
#main
root = Tk()
app = Application(root)
root.mainloop()

and the error is

Exception in Tkinter callback Traceback (most recent call last):
File "C:\Python31\lib\tkinter__init__.py", line 1399, in call return self.func(*args) File "D:/Python/practice/Guess_My_Number(GUI).py", line 43, in submit elif int(user_answer) > self.answer: TypeError: unorderable types: int() > Guessing_game()

How can I convert the Guessing game() object to int(), so I can compare the object and the int?

Upvotes: 0

Views: 664

Answers (1)

Steven Summers
Steven Summers

Reputation: 5394

It tells you exactly what the error is, you are comparing the object to an integer. Instead you should reference the variable inside the object

if int(user_answer) > self.answer.answer:
    ...

Might want to consider fixing the naming for this. Or consider if you even need that as a class. Additionally your __str__ method is incorrect, the return value needs to be a string whereas you are trying to return an integer (you can see this if you try print(Guessing_game(0, 100))

The following also doesn't quite do what you think

if user_answer != None:
    int(user_answer)

user_answer will always be a string since you are getting the value from an Entry widget.

Instead you probably want

# Check if the string is a number
if not user_answer.isdigit():
    # Some error message
    return # prevent trying anything else

You also need to reassign the result of int(user_answer) since it doesn't modify your variable it essentially does nothing ( unless it encounters an error )

user_answer = int(user_answer)

Upvotes: 1

Related Questions