运行此代码时
#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}$
不要将函数和实例变量的名称相同。在
改变
到
^{pr2}$我知道如果你来自java,你可以做这样的事情,它知道区别,但是在python中函数是一类公民,你可以直接引用它们。i、 电子邮箱:
但是我也可以覆盖它
这就是上面在init方法中设置
self.reserve_price
时发生的情况。在问题是您重写了
__init__
方法中的reserve_price
方法检查这个示例
解决办法很简单,改变其中一个的名字。在
相关问题 更多 >
编程相关推荐