简体   繁体   English

Python class(输入名称,output编号)

[英]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.

相关问题 python,输入数字,输出字母 - python , input number, output letter 输入输出浮点数问题python - input output floating number issue python Python-如果输入是字母或数字,则显示不同的输出 - Python - display different output if the input is a letter or a number Python-命名输出文件以包含部分输入文件名 - Python - name output file to include part of input file name python:为类实例变量名称添加一个数字 - python: adding a number for the class instance variable name 如何从输入目录文件夹输入并以与输入文件相同的名称将输出文件保存在python中的输出文件夹中 - How to input from input direcory folder and save output file in same name as input file in output folder in python Python - 遍历用户输入的数字,计算并输出输入的值 - Python - Loop through User input number, calculate and output the entered values 动态保存输出 csv 以匹配输入 csv python 的名称 - Dynamically save output csv to match name of input csv python python dataframe as function input and get another dataframe with new name as output - python dataframe as function input and get another dataframe with new name as output Python类条件:从x输入获取[x,x]输出? - Python class condition: getting an [x,x] output from an x input?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM