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

Mo Pengwah
  • 11
  • 1
  • 1
    The issue you're running up against is a variable-scope issue. There are multiple write-ups regarding this on the internet, [here is an example](https://www.datacamp.com/community/tutorials/scope-of-variables-python). The reason function `options` thinks `basket` is called before it's assigned, is because under `elif choice == '4'` you assign `basket` to an empty list. That creates a local variable. If you comment out that assignment, you can see that the program runs "correctly". – Hampus Larsson May 22 '20 at 12:58
  • You're right, I've just had a re-look at my code; thanks a bunch man – Mo Pengwah May 22 '20 at 17:06

1 Answers1

2

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.

Zain Arshad
  • 1,885
  • 1
  • 11
  • 26
  • Thanks a lot for this man, its very helpful - didn't know people on here would reply so fast ! – Mo Pengwah May 22 '20 at 17:06
  • New users must take this to account, that if they find the answer correct and it resolves their issue, then kindly mark that as answer. so that if someone have the same issue in future they can get a handy help without asking a new question. That's how things work here . Welcome to SO :) – Zain Arshad May 24 '20 at 09:23