import time
shop = {'Processor': {'p3':100, 'p5':120, 'p7':200},
'RAM': {'16gb':75, '32gb':150},
'Storage': {'1tb':50, '2tb':100},
'Screen': {'19"':65, '23"':120},
'Case': {'Mini Tower':40, 'Midi Tower':70},
'USB Ports': {'2':10, '4':20}}
basket = []
def main():
print('''
Welcome to the PC Component Store!
Here, we sell you everything you will need.
(Keep in mind that display prices do NOT include VAT; this is added in checkout)
''')
options()
def productchoice():
a = 0
ptype = ''
pspec = ''
print('''
We assume you have taken a look at our catalog. To add an item to your basket here, you must:
> INPUT the type of product you are looking for, e.g: "RAM"
> Then, INPUT the specification of that type of item you desire, e.g: "16gb"
> We will process your request, and OUTPUT whether it has been accepted.
> If this does not work, we will allow you to try again. If you wish to return to menu, enter "MENU"
at each input prompt.
''')
while a == 0:
ptype = input('Product: ')
pspec = input ('Specification: ')
if ptype.lower() == 'menu' and pspec.lower() == 'menu':
print('''Returning to menu...
''')
options()
elif a == 0:
totalcost = 0
try:
totalcost += shop[ptype][pspec]
except:
print('''Your request was invalid. Please try again, and if you are unsure, return to
menu and revisit our catalog.
''')
continue
else:
addtocart(ptype,pspec)
print(totalcost)
print(basket)
else:
print('''Your request was invalid. Please try again, and if you are unsure, return to
menu and revisit our catalog.
''')
continue
def addtocart(ptype,pspec):
itemstr = ptype + ' ' + pspec
basket.append(itemstr)
return basket
def options():
a = 0
while a == 0:
choice = input('''Do you want to:
"SEE CATALOG"
"CHOOSE PRODUCT TO ADD TO BASKET"
"VIEW BASKET"
"CLEAR BASKET"
"CHECKOUT"
Enter a valid option using a number from (1-5): ''')
if choice == '1':
a = 1
print('WORK IN PROGRESS')
elif choice == '2':
a = 1
productchoice()
elif choice == '3':
a = 1
print('Your basket contains: ')
print(basket)
elif choice == '4':
a = 1
basket = []
print('''Basket cleared.'''
)
options()
elif choice == '5':
a = 1
checkout()
else:
print('''You must select a valid option from (1-5), taking you back to the menu...
''')
continue
main()
It comes up with an error saying 'UnboundLocalError: local variable 'basket' referenced before assignment' when 'basket' is used in options 3 or 4. What does this mean? Why when 'basket' is used in option 2, it works perfectly? I'm a GCSE student going into A-level who isn't that great at coding, so I thought stackoverflow could help.. this is really frustrating and I need to do it for homework..