Python класс (имя входа, номер выхода) - PullRequest
0 голосов
/ 20 июня 2020

Я хочу создать код, который будет выводить сумму приза продукта. Пример ниже.

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, я хотел сократить строки кода.

1 Ответ

0 голосов
/ 20 июня 2020

Я думаю, вы ищете что-то вроде этого, обратите внимание, в приведенном ниже коде нет никаких проверок ошибок / валидации:

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())
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...