Respuesta :
In python:
budget = float(input("What is your budget? $"))
apple_price = (whatever value you choose)
cheese_price = (whatever value you choose)
bread_price = (whatever value you choose)
apple_quantity = 0
cheese_quantity = 0
bread_quantity = 0
total = 0
# Finished initializing each variable.
# The loop stops when the budget is less than or equal to zero. Hopefully at zero though.
while budget > 0:
what_type = input("What type of item would you like? (a/c/b) ")
if what_type == "a":
apple_quantity = int(input("How many apples would you like? "))
if apple_quantity * apple_price > budget:
print(f"You can't afford {apple_quantity} apples")
apple_quantity = 0
# If the price of apples exceeds what is available in the budget, we can't buy the apples. This is the same for bread and cheese.
budget -= (apple_quantity * apple_price)
total += apple_quantity
# The budget is modified to include the price of the food purhcased, and the total amount of items purchased is increased if the user can afford the item(s).
elif what_type == "c":
cheese_quantity = int(input("How much cheese would you like? "))
if cheese_quantity * cheese_price > budget:
print(f"You can't afford {cheese_quantity} pounds of cheese")
cheese_quantity = 0
budget -= (cheese_quantity * cheese_price)
total += cheese_quantity
elif what_type == "b":
bread_quantity = int(input("How much bread would you like? "))
if bread_quantity * bread_price > budget:
print(f"You can't afford {bread_quantity} loaves of bread")
bread_quantity = 0
budget -= (bread_quantity * bread_price)
total += bread_quantity
elif what_type == "exit":
break
# This is the sentinel value to exit the purchasing program whenever the user wants.
print(
f"The total amount spent: ${((bread_quantity * bread_price) + (cheese_quantity * cheese_price) + (apple_quantity * apple_price))}")
print(
f"You purchased {apple_quantity} apples worth ${apple_price * apple_quantity}, {bread_quantity} loaves of bread worth ${bread_price * bread_quantity}, and {cheese_quantity} pounds of cheese worth ${cheese_price * cheese_quantity}.")
print(f"You have ${budget} remaining on your budget.")
# The total amount spent, the amount of items and their prices, and the remaining budget is printed to the console.
I hope this helps! If you need any further assistance please comment, and I will do my best to help.
The program prompts user for his/her budget, items and quantity to be purchased and creates and invoice based on the input. The program written in python 3 goes thus :
budget = int(input('Enter your lunch budget : '))
# prompt for user's budgeted amount
price_inv = {'apple' : 3 , 'cheese' : 4 , 'bread' : 5 }
#dictionary for the price of items
cost_inv = {'apple' : 0 , 'cheese' : 0 , 'bread' : 0 }
#dictionary for the price of each items purchased
quantity = {'apple' : 0 , 'cheese' : 0 , 'bread' : 0 }
#quantity of each item purchased
total_sum = 0
#initialized value for the total sum
budget_left = budget
#initialized value for the amount left from user's budget.
while budget > 0 :
#a while loop which runs till budget is finished
item_type = input('enter the item to purchase : ')
if item_type == 'nothing_more' :
break
#allow user to break out of the loop before the condition is met
else :
units = int(input('units : '))
#prompts for the unit of items the user want
cost = units * price_inv[item_type]
#calculates the cost
cost_inv[item_type] += cost
#updates the cost dictionary
quantity[item_type] += units
#updates the number of units purchased
total_sum += cost
#updates the total cost
budget_left -=cost
#updates the budget left
budget -= cost
print('number of apples : ', quantity['apple'], 'Cost : ', cost_inv['apple'])
print('number of cheese : ', quantity['cheese'], 'Cost : ', cost_inv['cheese'])
print('number of bread : ', quantity['bread'], 'Cost : ', cost_inv['bread'])
print('Total cost : ', total_sum)
print('Budget left : ', budget_left)
#displays user's purchase information as an invoice
A sample run of the program is attached including the script.
Learn more :https://brainly.com/question/16403687

