[英]how do I properly print a sentence using variables defined in my own class in python
So I created a class in python called Pizza, which takes 4 arguments...I want to be able to insert the arguments and then print it so that it comes out in a clean sentence.所以我在 python 中创建了一个名为 Pizza 的 class,它需要 4 个 arguments...我希望能够插入一个 ZDBC11CAA5BDA8 出来的句子,然后它会出现 98F77E6FB4D。 For example if I put this into the terminal---
例如,如果我把它放入终端---
from Pizza import Pizza
appetizer = Pizza("Pepperoni", 16, 10.50, 10)
print(appetizer)
I would want to get this result--- Your Pepperoni pizza has a diameter of 16 inches, a price of $10.5, and 10 slices per pie我想得到这个结果——你的意大利辣香肠披萨直径为 16 英寸,价格为 10.5 美元,每个馅饼有 10 片
Unfortunately, with my code I keep getting this when I print out the variable---不幸的是,使用我的代码,当我打印出变量时,我不断得到这个——
<Pizza.Pizza object at 0x7f17b762f650>
Anyone know why this is happening?有谁知道为什么会这样? My code is below
我的代码如下
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")
Great question: It's possible to make the name of the object be a str function:好问题:可以将 object 的名称设为 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)
try尝试
from Pizza import Pizza
appetizer = Pizza("Pepperoni", 16, 10.50, 10).myfunc()
#you don't need to print appetizer because myfunc does it already
and also, change class Pizza to this并且,将 class Pizza 更改为此
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")
so it doesn't cause a "TypeError: can only concatenate str (not "int") to str" error所以它不会导致“TypeError: can only concatenate str (not "int") to str" 错误
You're telling Python to print the Pizza object, so it happily complies:).你告诉 Python 打印披萨 object,所以它很高兴遵守:)。 Also in your
myfunc()
method you're adding strings to numbers (this will cause an error).同样在您的
myfunc()
方法中,您正在向数字添加字符串(这将导致错误)。
You should try this instead:你应该试试这个:
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")
and和
from Pizza import Pizza
appetizer = Pizza("Pepperoni", 16, 10.50, 10)
appetizer.myfunc()
output: Your Pepperoni pizza has a diameter of 16 inches, a price of 10.5 and 10 slices per pie
output:
Your Pepperoni pizza has a diameter of 16 inches, a price of 10.5 and 10 slices per pie
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.