Распечатайте название товара, цену и количество с примерами кода

Чтобы распечатать имя товара, цену и количество с помощью метода, вы можете создать класс с атрибутами для имени товара, цены и количества, а затем определить метод внутри этого класса для печати значений. Вот пример на Python:

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

    def print_details(self):
        print("Item name:", self.name)
        print("Price:", self.price)
        print("Quantity:", self.quantity)
# Creating an instance of the Item class
item = Item("Example Item", 10.99, 5)
# Calling the print_details() method to print the item details
item.print_details()

Это выведет:

Item name: Example Item
Price: 10.99
Quantity: 5

Вот несколько дополнительных методов с примерами кода для печати названия товара, цены и количества:

  1. Использование функции:

    def print_item_details(name, price, quantity):
    print("Item name:", name)
    print("Price:", price)
    print("Quantity:", quantity)
    # Calling the function to print the item details
    print_item_details("Example Item", 10.99, 5)
  2. Использование форматированной строки:

    def print_item_details(name, price, quantity):
    print(f"Item name: {name}")
    print(f"Price: {price}")
    print(f"Quantity: {quantity}")
    # Calling the function to print the item details
    print_item_details("Example Item", 10.99, 5)
  3. Использование словаря:

    def print_item_details(item):
    print("Item name:", item["name"])
    print("Price:", item["price"])
    print("Quantity:", item["quantity"])
    # Creating a dictionary for the item
    item = {
    "name": "Example Item",
    "price": 10.99,
    "quantity": 5
    }
    # Calling the function to print the item details
    print_item_details(item)