Why will the variable 'basket' not be recognised when the choices 3 or 4 are chosen?

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..

1 answer

  • answered 2020-05-22 13:06 Zain Arshad

    You are facing a very common issue for most of new python developers and it is variable-scope problem. So, every variable has a its own scope, which means it can be referenced at some point in code in a very specific way. You can read here more about it.

    As far as your problem holds, you can use a global key word at the start of function. Add this line:

    global basket
    

    at line # 91 and your program will run perfectly. This lines tells python to use the global basket list that you have created on line # 10.

    Remember:

    It should be noted that, use of global all around the code is not so much appreciated in python community, as it can lead to very complex code, if your code is very large and your modifying the global variable all across the code. If this is the case then its time to re-structure your code.