2017-11-12 15 views
1

私はちょうどプログラミングを始めました。私はコーディングの最初の試みにPythonを使用することに決めました。クラスとオブジェクトで練習しています。 私が尋ねようとしている質問が以前に尋ねられたのであれば謝りますが、私はどこでも答えを見つけることができないと思われます。ユーザー入力からクラスオブジェクトとその属性にアクセスするにはどうすればよいですか?

私はクラスを含むファイルを持っています。同じディレクトリに保存された別のファイルでは、

#class file 
#class prodotti refers to "register" with products in stock and their prices 

class Prodotti(): #class Prodotti() contains products from register and their relative specs 
def __init__(self, nome="", #name of product 
        prezzo=0, #product price 
        quantità=0,): #stock quantity of product 
    self.nome=nome 
    self.prezzo=prezzo 
    self.quantità=quantità 

def newproduct(self): #method appends new product and its specs to the end of this file 
     name=input("Inserire nuovo prodotto: ") 
     f=open("cassa3.py", "a") 
     f.write(name + "=Prodotti(nome='" + name + "', ") 
     price=input("Inserire prezzo prodotto: ") 
     f.write("prezzo=" + price + ", quantità=0)\n") 
     f.close() 

def tellprice(self): #method should return price of object 
    inp=input("Di quale prodotto vuoi conoscere il prezzo? ") #asks user which product they want to know the price of 
    if inp=Prodotti(): 
     print(inp.prezzo) 

#class objects 
#user can insert new products that are saved below 
tortino=Prodotti(nome="Tortino al cioccolato", prezzo=3.4, quantità=0) 
muffincioccolato =Prodotti(nome="Muffin al cioccolato", prezzo=1.8, quantità=0) 
cupcake=Prodotti(nome='cupcake', prezzo=2, quantità=0) 

、私はメインプログラムがあります:私は書かれている完全なコード以下は、私が欲しいもの、上記のコードから教えているよう

from cassa3 import Prodotti #file cassa3.py in same directory as this file 



if __name__=="__main__": 
P=Prodotti() 
P.tellprice() 

をメソッドtellprice()は、その製品の価格を知りたい製品をユーザーに尋ねることです。 しかし、ユーザー入力をクラスオブジェクトに対応させる方法がわからないので、その属性にアクセスできます。 誰かがそれをどうやってできるのか説明できますか?

ありがとうございます。

答えて

1

この問題を解決するには、まず問題を解決する必要があります。

あなたのコメントには# class Prodotti() contains products from register and their relative specsと表示されていますが、それはあまり真実ではありません。このクラスには、名前、価格、数量を持つ単一の製品が含まれています。

プロダクト名(Prodottiのインスタンス)の実際のリスト(またはプロダクト名が効率的なルックアップのためにユニークなものなど)を実際に格納する別のクラス(おそらくRegister)を定義する必要があります。

tellpriceの方法は現在意味がありません。 Prodottiの新しいインスタンスが作成され、ifの条件はとなることはありません。Trueになります。

また、コードで英語名を使用することを強くお勧めします。

は一般的なガイドとして以下の例を考えてみましょう:

class Product: 
    def __init__(self, name, price, quantity): 
     self.name = name 
     self.price = price 
     self.quantity = quantity 

    # (... some other methods ...) 

class Register: 
    def __init__(self, products): 
     # this will store the products in a dictionary with products names as keys 
     # and Product instances as values for an efficient look up by tell_price 
     self.products = {product.name: product for product in products} 

    def tell_price(self): 
     name = input('Which product would you like to know the price of?') 
     # this will raise KeyError if user inputs a non-existing product name 
     # and should be caught, or use .get(...) instead 
     return self.products[name].price 


apple = Product('apple', 1, 10) 
banana = Product('banana', 2, 2) 

register = Register([apple, banana]) 

print(register.tell_price()) 

# Which product would you like to know the price of? 
>> apple 
# 1 
+0

私が辞書を使いたくない理由は、さらにアクセスしたい複数のパラメータ(価格だけでなく数量など)があるからです。したがって、私はそれをすべてカバーするために複数の辞書を作成する必要があります。私が知りたいのは、ユーザーの入力を使ってクラスの新しいオブジェクトを作成する方法があれば、私はあなたの提案ではできないか、少なくとも私が見ることはできないことです:))。 – Leena

+0

@Leenaいいえ、複数の辞書は必要ありません。詳しく見て、 'Product'クラスがすべての情報を格納していることを確認してください。 'tell_price'が' self.products [name] .price'を返すのと同じように、 'tell_quantity'メソッドは' self.products [name] .quantity'を返すことができます。あなたの質問は、ユーザーの入力に基づいてオブジェクトを作成することではなく、情報を取得することでした。 – DeepSpace

+0

ユーザー入力から新しいクラスオブジェクトを作成する方法はありますか?だから私はそれをどうやってやるの?編集:オリジナルの質問ではなく、私は単にあなたがそれを行う方法を疑問に思っています。私のコードの残りの部分があなたの提案に合わないからです:) – Leena

-1

私はあなたのtellpriceがユーザーの入力を含むことはないだろう。メインで次に

def tellprice(self): #method should return price of object 
    return self.price 

(これが大幅に簡略化された):

inp = input("Di quale prodotto vuoi conoscere il prezzo? ") 
print(inp.tellprice) 

明らかにこれは彼らが正しい製品名入れを前提としていなので、彼らが間違っていることをユーザに示すいくつかの方法があるかもしれないが便利だ

関連する問題