
Сделайте так, чтобы пользователь вводил название товара и его цену в одной строке. Например,
"Macbook 1500". питон my_basket = {} ware_1 = input("product name - ") my_basket[ware_1] = int(input("value - ")) ware_2 = input("product name - ") my_basket[ware_2] = int(input("value - ")) ware_3 = input("product name - ") my_basket[ware_3] = int(input("value - ")) ware_4 = input("product name - ") my_basket[ware_4] = int(input("value - ")) ware_5 = input("product name - ") my_basket[ware_5] = int(input("value - ")) total_cost = sum(my_basket.values()) print('We bought these goods:') print('ware -', my_basket.popitem()) print('ware -', my_basket.popitem()) print('ware -', my_basket.popitem()) print('ware -', my_basket.popitem()) print('ware -', my_basket.popitem()) print("Total cost -", total_cost)

Ответы на вопрос

my_basket = {}
for _ in range(5):
prod = input().split()
my_basket[prod[0]] = int(prod[1])
total_cost = sum(my_basket.values())
print("We bought these goods:")
for _ in range(5):
print("Ware -", my_basket.popitem())
print("Total cost -", total_cost)



Python code to allow the user to enter the product name and its price in a single line:
To allow the user to enter the product name and its price in a single line, you can modify the code as follows:
```python my_basket = {} products = input("Enter product names and prices (separated by space): ").split()
for i in range(0, len(products), 2): product_name = products[i] product_price = int(products[i+1]) my_basket[product_name] = product_price
total_cost = sum(my_basket.values())
print('We bought these goods:') for product, price in my_basket.items(): print('Product:', product, '- Price:', price)
print("Total cost:", total_cost) ```
This code will prompt the user to enter the product names and prices, separated by a space. It will then split the input string into a list of product names and prices. The code will iterate over the list and assign each even-indexed element as the product name and the following odd-indexed element as its price. The product name and price are then added to the `my_basket` dictionary.
After all products are added, the code calculates the total cost by summing the values of the `my_basket` dictionary. Finally, it prints the list of goods purchased and the total cost.
Please note that in your initial code, there are a few issues with the syntax and logic. I have corrected those issues in the code provided above.


Похожие вопросы
Топ вопросов за вчера в категории Информатика







Последние заданные вопросы в категории Информатика
-
Математика
-
Литература
-
Алгебра
-
Русский язык
-
Геометрия
-
Английский язык
-
Химия
-
Физика
-
Биология
-
Другие предметы
-
История
-
Обществознание
-
Окружающий мир
-
География
-
Українська мова
-
Информатика
-
Українська література
-
Қазақ тiлi
-
Экономика
-
Музыка
-
Право
-
Беларуская мова
-
Французский язык
-
Немецкий язык
-
МХК
-
ОБЖ
-
Психология
-
Физкультура и спорт
-
Астрономия
-
Кыргыз тили
-
Оʻzbek tili