-3

I am fairly new to Python/programming, I started about four months ago and since I am a teacher and may possible have a lot more time on my hands.

For my first project I have started to develop a pizza ordering system-- at the moment I am just having the user interact with the program through the terminal. However, I plan to evidentially use Django to create a web page so a user can actually interact with.

I will first share my code:

import re

sizePrice = {
    'small': 9.69, 
    'large': 12.29, 
    'extra large': 13.79, 
    'party size': 26.49 }

toppingList = [ 'Anchovies', 'Bacon', 'Bell Peppers', 'Black Olives',
'Chicken', 'Ground Beef', 'Jalapenos', 'Mushrooms',
'Pepperoni','Pineapple', 'Spinach']

pickupCost = 0 deliveryCost = 5.0


running = True 


def pick_or_delivery():
    global delivery 
    print('\nWill this be for pick up or delivery?')
    delivery = input("P - pick up / D - delivery")
    delivery = delivery.title() # changes the letter inputted to an upper case. 
    print(f'This order will be for {delivery}')

    if delivery == "D":
        while running == True:
            global customerName 
            customerName = input("\nName for the order: ")
            if not re.match("^[a-zA-Z ]*$", customerName):
                print("Please use letters only")
            elif len(customerName) == 0:
                print("Please enter a vaild input")
            else: 
                customerName = customerName.title()
                break

        while running == True:
            global customerPhoneNumber
            customerPhoneNumber = input("\nEnter a phone number we can contact you at: ")
            if not re.match("^[0-9 ]*$", customerPhoneNumber): 
                print("Please use numbers only")
            elif len(customerPhoneNumber) == 0: 
                print("Please enter a a contact phone number")
            else: 
                break 
            
        while running == True:
            global streetName
            streetName = input("Street name: ")
            if not re.match("^[a-zA-Z ]*$", streetName): 
                print('Please use letters only.')
            elif len(streetName) == 0: 
                print("Please enter a valid input")
            else:
                streetName = streetName.title()
                break

    elif delivery == "P":
        while running == True:
            global customerName 
            customerName = input("\nName for the order: ")
            if not re.match("^[a-zA-Z ]*$", customerName): 
                print("Please use letters only")
            elif len(customerName) == 0: 
                print("Please enter a valid input")
            else: 
                print("Please enter P or D")
                customerName = customerName.title()
                break
        
        while running == True:
            global customerPhoneNumber
            customerPhoneNumber = input("\nEnter a phone number we can contact you at: ")
            customerName = customerName.title()
            if not re.match("^[0-9 ]*$", customer_telephone): 
                print("Please use numbers only")
            elif len(customer_telephone) == 0:  
                print("Please enter a valid input")
            else:
                break 
    else:
        print("Please enter P or D ")
        pick_or_delivery()

pick_or_delivery()

When I run this code I receive the following:

SyntaxError: name 'customerName' is used prior to global declaration

I know the issue is in my global variables, however in my code I am placing the global variable before it is being called. So I am a little confused. Is the placement incorrect? If so, where should it go?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 4
    Please don't put your code in block quotes. It makes your code unintelligible. See [How do I format my code blocks?](https://meta.stackexchange.com/q/22186/628368) – khelwood Jul 16 '20 at 23:24
  • Is there a reason you're using globals? They're usually frowned upon for simple applications like this [why are globals bad?](https://stackoverflow.com/questions/19158339/why-are-global-variables-evil). – DarrylG Jul 16 '20 at 23:41
  • I will be using the globals to print out a sort of receipt with all the needed information at the end of the program. I will take a look at the like thank you for the help. @DarrylG – Chris Jaurigue Jul 16 '20 at 23:46
  • 1
    Issue seems to be declaring the `global customerName` multiple times in the function. I would suggest you declare all the globals at the beginning of the function to make the function easier to follow and it removes the error you're getting. – DarrylG Jul 16 '20 at 23:52
  • @khelwood As stated in my post I am new and this was my first post, but thanks for the advise. – Chris Jaurigue Jul 16 '20 at 23:53
  • 1
    @ChrisJaurigue--actually congratulations on your first post and hopefully you got some good feedback. My guess to why your question was probably downvoted (not by me) was you ran into a couple of pet peeves for posting. Namely: 1) not formatting code properly in the question and 2) using globals in your code. Either of these will normally cause reviewers to grimace. Would suggest you rewrite without globals as a learning exercise and also to use [PEP 8](https://www.python.org/dev/peps/pep-0008/) style guide for variable naming. – DarrylG Jul 17 '20 at 00:20

1 Answers1

-1

I think it is because you have globalised customerName within your if statements. Try and create a variable called customerName at the start of your function and give it a blank value, then globalise it at the start of your function.

halfer
  • 19,824
  • 17
  • 99
  • 186
dylanvh
  • 53
  • 1
  • 9