python 3.x Type Error int object is not callable

2024-09-30 02:28:59 发布

您现在位置:Python中文网/ 问答频道 /正文

运行此代码时

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

我收到错误

^{pr2}$

Tags: nameselfformatifisdefpriceprint
2条回答

不要将函数和实例变量的名称相同。在

改变

def reserve_price(self):

^{pr2}$

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

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

但是我也可以覆盖它

In [6]: x = 5

In [7]: x
Out[7]: 5

这就是上面在init方法中设置self.reserve_price时发生的情况。在

问题是您重写了__init__方法中的reserve_price方法

检查这个示例

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

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

解决办法很简单,改变其中一个的名字。在

相关问题 更多 >

    热门问题