Софтуерно Инженерство
Loading...
TeodorPavlov avatar TeodorPavlov 5 Точки

Задача 4. Туристически магазин

Здравейте, имам проблем със следната задача: 

https://softuni.bg/downloads/svn/programming-basics-v4/course-directories/may-2019/2019-03/Retake-Exam/04.%20Tourist%20Shop.pdf

Това е моето решение:

https://pastebin.com/Xa2akj0p

Ако някой може да помогне, ще съм много благодарен.

 

mark79 avatar mark79 458 Точки

Здравей,

едно решение на задачата

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!")

 

0
26/08/2019 18:43:20
TeodorPavlov avatar TeodorPavlov 5 Точки

Благодаря за отговора. 

0