[英]Python class (input name,output number)
i want to create code which will be outputing the sum of the product prize.我想创建将输出产品奖品总和的代码。 Example below.
下面的例子。
Input:
bread,rice
Output:
6,5 $
Here is my code:这是我的代码:
class product:
def __init__(self,name,color,prize)
self.name=name
self.color=color
self.prize=prize
def info(self)
input(name)
return (self.prize)
tomato=product()
tomato.name="tomato"
tomato.color="czerwony"
tomato.prize= 4,56
turnip=product()
turnip.name="turnip"
turnip.color="white"
turnip.prize= 5.65
print("write the name of one products from the list and I will tell you how much it costs [tomato,turnip] "
info()
There is only 2 products,i wanted to reduce code lines.只有 2 个产品,我想减少代码行。
I think you are looking for something like this, please note, the code below does not have any error/validation checks in place:我认为您正在寻找类似的东西,请注意,下面的代码没有任何错误/验证检查:
class product:
def __init__(self,name=None,color=None,prize=None):
self.name=name
self.color=color
self.prize=prize
def info(self):
return (self.prize)
tomato=product()
tomato.name="tomato"
tomato.color="czerwony"
tomato.prize= 4.56
turnip=product()
turnip.name="turnip"
turnip.color="white"
turnip.prize= 5.65
user_input = raw_input("write the name of one products from the list and I will tell you how much it costs [tomato,turnip]:")
print(eval(user_input).info())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.