'2 code parts connection and register of letters in list
I'm writing a code for running shopping program, where you can write the product which you need to buy and it will be added to cart, where total cost will show up. Also i wrote a code so it could make budget warnings if the total cost is getting close to budget border (70% and more).
I want to connect this 2 parts of code so they work together, and also I want to make sure that the entered product is counted regardless of the register (so it would be no matter if it is 'Sause' or 'SAUSe' in input)
Will be greatful for your help!
#PRODUCT CART
productlist = ['Sushi', 'Spirulini', 'Sause', 'Cabbage']
pricelist = [56, 31, 4, 9]
totalcost = 0
inpt = input('Add to cart: ')
#indx = productlist.index(inpt)
#price = pricelist[indx]
endWord = 'stop'
spacing = ''
while inpt.lower() != endWord.lower():
if inpt not in productlist and len(inpt) > 0:
print("\nProduct is not found, try again")
inpt = input('\nAdd to cart: ')
elif inpt in productlist:
indx = productlist.index(inpt)
price = pricelist[indx]
totalcost += price
print("\n{} costs {:d} usd".format(inpt, price))
print (f'Total cost is {totalcost}')
inpt = input('\nAdd to cart: ')
elif spacing in inpt:
inpt = input('\nAdd to cart: ')
print ('\nThank you for using ShopCart.com!')
#BUDGET WARNINGS
#budget = 150 #actually, it is better to make it as input
#moreThan70Line = int(totalcost / budget * 100)
#if totalcost >= (70/100 * budget) and totalcost < budget:
# print (f'Look out, you are on the {moreThan70Line}% of your budget')
#elif totalcost >= budget:
# print (f'Seems like you used {moreThan70Line}% of #your today\'s budget')
Solution 1:[1]
You can use a check_budget function that checks and alerts your user while they add items to cart.
# BUDGET WARNINGS
def check_budget(total_cost, budget):
percentage_used = int(totalcost / budget * 100)
if percentage_used >= 70 and total_cost < budget:
print(f'Look out, you are on the {percentage_used}% of your budget')
elif total_cost >= budget:
print(f'Seems like you used {percentage_used}% of #your today\'s budget')
This lets you use the helper function check_budget inside your product cart code something like this.
Also, you can keep the items in productlist in lower case and convert your user input into lower case to achieve case insensitivity for your products.
# PRODUCT CART
productlist = ['Sushi', 'Spirulini', 'Sause', 'Cabbage']
pricelist = [56, 31, 4, 9]
totalcost = 0
inpt = input('Add to cart: ')
endWord = 'stop'
spacing = ''
budget = int(input('\nEnter budget: '))
while inpt.lower() != endWord.lower():
inpt = inpt.lower()
if inpt not in productlist and len(inpt) > 0:
print("\nProduct is not found, try again")
inpt = input('\nAdd to cart: ')
elif inpt in productlist:
indx = productlist.index(inpt)
price = pricelist[indx]
totalcost += price
print("\n{} costs {:d} usd".format(inpt, price))
print (f'Total cost is {totalcost}')
# budget check and alert
check_budget(totalcost, budget)
inpt = input('\nAdd to cart: ')
elif spacing in inpt:
inpt = input('\nAdd to cart: ')
print ('\nThank you for using ShopCart.com!')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
