Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3.x-“;类型错误:';int';对象不可调用";_Python_Python 3.x - Fatal编程技术网

Python3.x-“;类型错误:';int';对象不可调用";

Python3.x-“;类型错误:';int';对象不可调用";,python,python-3.x,Python,Python 3.x,运行此代码时 #Silent Auction class Auction: def __init__(self): self.reserve_price = 30 self.highest_bid = 0 self.highest_bidder = "" self.namelist = [] self.bidlist = [] d

运行此代码时

 #Silent Auction

    class Auction:

        def __init__(self):
            self.reserve_price = 30
            self.highest_bid = 0
            self.highest_bidder = ""
            self.namelist = []
            self.bidlist = []

        def reserve_price(self):
            print("Hello. The reserve price is ${}".format(self.reserve_price))

        def new_bidder(self):
            LOOP = 0
            while LOOP == 0:
                name = input("What is your name? 'F' for FINISH ")
                if name.upper() == "F":
                    LOOP = 1
                else:
                    bid = int(input("Hello {}. What is your bid? ".format(name)))
                    if bid > self.highest_bid:
                        self.highest_bid = bid
                        self.highest_bidder = name
                        self.namelist.append(name)
                        self.bidlist.append(bid)

                    else:
                        print("Sorry {}. You'll need to make another higher bid.".format(name))
                        print("Highest bid so far is ${:.2f}".format(self.highest_bid))


        def auction_end(self):
            if self.highest_bid >= self.reserve_price:
                print("The auction met the reserve price and the highest bidder was {} with ${:.2f}".format(self.highest_bidder, self.highest_bid))
            else:
                print("The auction did not meet the reserve price")
                n = len(self.namelist)
                for i in range (0, n):
                    print("{} bid ${:.2f}".format(self.namelist[n], self.bidlist[n]))

    if __name__ == "__main__":
        auction1 = Auction()
        auction1.reserve_price()
        auction1.new_bidder()
        auction1.auction_end()
我收到了错误

Traceback (most recent call last):
  File "F:\Work\Year 13\13DIP\91637 Programming\Silent Auction.py", line 46, in <module>
    auction1.reserve_price()
TypeError: 'int' object is not callable
>>> 
回溯(最近一次呼叫最后一次):
文件“F:\Work\Year 13\13DIP\91637 Programming\Silent Auction.py”,第46行,在
拍卖1.底价()
TypeError:“int”对象不可调用
>>> 

不要将函数和实例变量命名为同一个名称

改变

def reserve_price(self):

我知道如果你来自java,你可以做这样的事情,它知道其中的区别,但在python中,函数是一流的公民,你可以直接引用它们。i、 e:

In [2]: x = lambda i : i * i
In [3]: x
Out[3]: <function __main__.<lambda>>
In [5]: x(2)
Out[5]: 4

这就是当您在init方法中设置self.reserve\u price时发生的情况。

问题是您在
\u init\u
方法中覆盖了
reserve\u price
方法

检查这个示例

>>> class A:
        def __init__(self):
            self.fun = 42
        def fun(self):
            print( "funny" )

>>> a = A()
>>> a.fun
42
>>>

解决方案很简单,请更改其中一个的名称

这不是免费的调试服务。你需要做的工作比转储整个代码和堆栈跟踪多一点。这是一种糟糕的询问行为,我只是为了鼓励你这么做。对不起,没有必要为你的观点道歉;这就是社区的意义所在。我只是倾向于认为,如果问题有足够的代码在足够小的上下文中用准确的错误消息重现错误,并且用户可能是新用户,我还可以帮助他们。我确实认为我可以在回答问题时提供更多的背景。我自己也有很多“哦,哇”的时刻,另一双眼睛经常能捕捉到。哦,哇,我现在感觉好傻。我不完全理解输入/输出的内容,但我理解你的意思,即我的函数名不好。谢谢
In [6]: x = 5

In [7]: x
Out[7]: 5
>>> class A:
        def __init__(self):
            self.fun = 42
        def fun(self):
            print( "funny" )

>>> a = A()
>>> a.fun
42
>>>