【问题标题】:how do I properly print a sentence using variables defined in my own class in python如何使用python中我自己的类中定义的变量正确打印句子
【发布时间】:2020-08-01 01:53:46
【问题描述】:

所以我在 python 中创建了一个名为 Pizza 的类,它接受 4 个参数...我希望能够插入参数然后打印它,以便它以一个干净的句子出现。例如,如果我把它放到终端---

from Pizza import Pizza
appetizer = Pizza("Pepperoni", 16, 10.50, 10)
print(appetizer)

我想得到这个结果--- 您的意大利辣香肠披萨直径为 16 英寸,价格 10.5 美元,每个馅饼 10 片

不幸的是,当我打印出变量时,我的代码不断得到这个---

<Pizza.Pizza object at 0x7f17b762f650>

有人知道为什么会这样吗?我的代码如下

class Pizza:
    def __init__(self, name, diameter, price, slices):
        self.name = name
        self.diameter = diameter
        self.price = price
        self.slices = slices
    def myfunc(self):
        print("Your" +  self.name + "pizza has a diameter of" + self.diameter + "inches, a price of" + self.price + "and" + self.slices + "slices per pie")

【问题讨论】:

  • 查找__str____repr__ 方法。

标签: python function class variables self


【解决方案1】:

好问题!可以将对象的名称设为 str 函数:

class Pizza:
    def __init__(self, name, diameter, price, slices):
        self.name = name
        self.diameter = diameter
        self.price = price
        self.slices = slices
    def __repr__(self):
        return "Your {} pizza has a diameter of {} inches, a price of {} and {} slices per pie".format(self.name,self.diameter,self.price,self.slices)


appetizer = Pizza("Pepperoni", 16, 10.50, 10)
print(appetizer)

【讨论】:

  • 请注意,如果您使用的是 Python 3.6+,则可以使用 f-strings:f"Your {self.name} pizza has a diameter of {self.diameter} inches, a price of {self.price} and {self.slices} slices per pie." 通常比 .format() 更容易跟踪。
【解决方案2】:

试试

from Pizza import Pizza
appetizer = Pizza("Pepperoni", 16, 10.50, 10).myfunc()
#you don't need to print appetizer because myfunc does it already

另外,将披萨类更改为此

class Pizza:
    def __init__(self, name, diameter, price, slices):
        self.name = name
        self.diameter = diameter
        self.price = price
        self.slices = slices
    def myfunc(self):
        print("Your " +  self.name + " pizza has a diameter of " + str(self.diameter) + " inches, a price of " + str(self.price) + " and " + str(self.slices) + " slices per pie")

所以它不会导致“TypeError: can only concatenate str (not "int") to str”错误

【讨论】:

    【解决方案3】:

    你告诉 Python 打印 Pizza 对象,所以它很高兴地遵守 :)。同样在您的 myfunc() 方法中,您正在向数字添加字符串(这将导致错误)。

    你应该试试这个:

    class Pizza:
        def __init__(self, name, diameter, price, slices):
            self.name = name
            self.diameter = diameter
            self.price = price
            self.slices = slices
        def myfunc(self):
            print("Your", self.name, "pizza has a diameter of", self.diameter, "inches, a price of", self.price, "and", self.slices, "slices per pie")
    

    from Pizza import Pizza
    appetizer = Pizza("Pepperoni", 16, 10.50, 10)
    appetizer.myfunc()
    

    输出: Your Pepperoni pizza has a diameter of 16 inches, a price of 10.5 and 10 slices per pie

    【讨论】:

    • 由于某种原因它给了我这个错误...... TypeError: can only concatenate str (not "int") to str
    • @samdisorbo 确保将 myfunc() 更改为我或其他用户发布的内容。如果你在字符串和数字之间尝试 + 你会得到这个错误。您可以在 print() 参数之间使用逗号,或者使用 str() 将 print 语句中的每种类型转换为字符串。或像其他用户建议的那样使用 .format()。
    • 好的,我已经准备好了,但是现在我正在尝试实现新变量,例如每平方英寸的成本,但是当我调用它们时,我得到 'str' is not callable 错误... self.cost_per_square_inch = float((price/(((diameter/2)**2)*math.pi)))...这是我用来定义变量的,然后我通过做appetizer.cost_per_square_inch来调用它。 ...有机会再帮忙吗?
    • 嗯,你是调用变量还是调用 print(appetizer.cost_per_square_inch)?该变量似乎不是字符串,但我不确定您是否已将其转换。
    猜你喜欢
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    • 2014-05-07
    相关资源
    最近更新 更多