TypeError:unorderable类型:int()>猜谜游戏()

时间:2016-08-24 03:57:29

标签: python-3.x

我正在研究python并试图创建一个与GUI连接的猜测数字程序。 但是,有bug,我不知道如何解决,所以请帮助我。

我的代码是

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()

,错误是

  

Tkinter回调中的异常回溯(最近一次调用最后一次):
  文件“C:\ Python31 \ lib \ tkinter__init __。py”,第1399行,调用       return self.func(* args)提交文件“D:/ Python / practice / Guess_My_Number(GUI).py”,第43行       elif int(user_answer)&gt; self.answer:TypeError:unorderable类型:int()&gt; Guessing_game()

如何将Guessing game()对象转换为int(),以便我可以比较 对象和int?

1 个答案:

答案 0 :(得分:1)

它准确地告诉您错误是什么,您将对象与整数进行比较。相反,你应该在对象

中引用变量
if int(user_answer) > self.answer.answer:
    ...

可能要考虑为此修复命名。或者考虑一下你是否需要它作为一个班级。此外,您的__str__方法不正确,返回值必须是字符串,而您尝试返回一个整数(如果您尝试print(Guessing_game(0, 100))

,可以看到这一点

以下也不是你想的那样

if user_answer != None:
    int(user_answer)

user_answer将始终是一个字符串,因为您从Entry小部件中获取值。

相反,你可能想要

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

您还需要重新分配int(user_answer)的结果,因为它不会修改您的变量,它基本上什么都不做(除非它遇到错误)

user_answer = int(user_answer)