Задача 4. Туристически магазин
Здравейте, имам проблем със следната задача:
Това е моето решение:
Ако някой може да помогне, ще съм много благодарен.
Здравейте, имам проблем със следната задача:
Това е моето решение:
Ако някой може да помогне, ще съм много благодарен.
Здравей,
едно решение на задачата
budget = float(input())
itemCount = 0
itemTotalPrice = 0
while True:
    command = input()
    if command == "Stop":
        print(f"You bought {itemCount} products for {itemTotalPrice:.2f} leva.")
        break
    itemPrice = float(input())
    itemCount = itemCount + 1
    itemTotalPrice = itemTotalPrice + itemPrice / 2 if itemCount % 3 == 0 else itemTotalPrice + itemPrice
    if itemTotalPrice > budget:
        print(f"You don't have enough money!\nYou need {itemTotalPrice - budget:.2f} leva!")
        break
или, ако е по-ясно така
budget = float(input())
itemCount = 0
itemTotalPrice = 0
command = input()
while command != "Stop":
    itemPrice = float(input())
    itemCount = itemCount + 1
    if itemCount % 3 == 0:
        itemTotalPrice = itemTotalPrice + itemPrice / 2
    else:
        itemTotalPrice = itemTotalPrice + itemPrice
    if itemTotalPrice > budget:
        break
    command = input()
if itemTotalPrice > budget:
    print(f"You don't have enough money!\nYou need {itemTotalPrice - budget:.2f} leva!")
else:
    print(f"You bought {itemCount} products for {itemTotalPrice:.2f} leva.")
Поздрави :)
PS -> ето и корекциите по твоя код
budget = float(input())
count = 0
total = 0
while budget >= 0:
    product_name = input()
    if product_name == "Stop":
        print(f"You bought {count} products for {total:.2f} leva.")
        exit()
    product_price = float(input())
    count = count + 1
    if count % 3 == 0:
        product_price = product_price / 2
    budget = budget - product_price
    total = total + product_price
print("You don't have enough money!")
print(f"You need {abs(budget):.2f} leva!")
Благодаря за отговора.