簡體   English   中英

Python class(輸入名稱,output編號)

[英]Python class (input name,output number)

我想創建將輸出產品獎品總和的代碼。 下面的例子。

Input:
bread,rice

Output:
6,5 $

這是我的代碼:

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()

只有 2 個產品,我想減少代碼行。

我認為您正在尋找類似的東西,請注意,下面的代碼沒有任何錯誤/驗證檢查:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM