Jump to content

Need help with python program

Ok, so I am going to start off with the fact that I have been studying Python in school for about 2 years now (Year 11 in UK), and I have come across errors that have been frustrating me for quite some time now.

Here it is:

 

 

def welcomeScreen():
    #welcome message
    print("#####################")
    print("Welcome to Caroline's Painting/Decorating Company Program")
    print("#####################")
    print()
    #options menu
    print(" A | Enter employee details")
    print(" B | Enter customer details")
    print(" C | Estimation")
    print(" D | Price Calculator")
    print()
    option=input("Please select an input option: ")
    if option=="A" or option=="a":
        #enter employee details
        print ()
        print ("Employee Details")
        print ()
        for line in employeeInfo:
            employeeName,hireDate,birthdate=line.split(",")
            
        print("The employee's name is:",employeeName)
        print("The employee was hired on:",hireDate)
        print("The employee's birthdate is:",birthdate)
    elif option=="B" or option=="b":
        print("not implimented yet")
        welcomeScreen()

    elif option=="C" or option=="c":
        print("not implimented yet")
        welcomeScreen()
        
    elif option=="D" or option=="d":
        print("not implimented yet")
        welcomeScreen()

file=open("employeeInfo.txt","r")
employeeInfo=file.readlines()
file.close()
welcomeScreen()

 

 

the text document employeeInfo.txt looks like:

 

Dave Newman,11/3/03,1/2/1997
Jonny Stewart,15/12/15,22/5/1990

 

note, the welcomeScreen varibles inside of the elifs are just temporary, until I put code there :P

 

the error that comes up:

 

Traceback (most recent call last):
  File "Z:/Documents/Python/Exam Paper/Task 3/task 3-v1.py", line 45, in <module>
    welcomeScreen()
  File "Z:/Documents/Python/Exam Paper/Task 3/task 3-v1.py", line 27, in welcomeScreen
    print("The employee's name is:",employeeName)
UnboundLocalError: local variable 'employeeName' referenced before assignment

 

Also, I need to put an input in there to get the user to input a name and have it check against the document, and I haven't been able to find a solution.

 

Thanks for your help!!

Spoiler

Future build:

http://uk.pcpartpicker.com/p/kGgNJx

 

Current build: (laptop)

 

Use the following style specs for your profile!
Specs:
Screeninator: Intel Graphics 5500
Stickaminator: Hyandai 12GB DDR3 RAM (1x8GB, 1x4GB)
Procrastinator: Intel i5-5500U @ 2.2 GHz
Noisoundacreator: TURTLE BEACH PX4
Remembrerthing: WD 1TB Blue HDD
Flat-Colorful-Thing: Samsung SyncMaster 2323Y
See-A-Move-O: Tecknet M288

 

Link to comment
Share on other sites

Link to post
Share on other sites

i dont have python installed to test myself, but i think its because this

 

for line in employeeInfo:
            employeeName,hireDate,birthdate=line.split(",")

 

is making local variables which cant be accessed by the elif statements outside the for loop

you need to make those things a variable at the start of of function welcomescreen and it should work

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, madknight3 said:

The code worked when I ran it.

mine isn't working, using python 3.5.1

Spoiler

Future build:

http://uk.pcpartpicker.com/p/kGgNJx

 

Current build: (laptop)

 

Use the following style specs for your profile!
Specs:
Screeninator: Intel Graphics 5500
Stickaminator: Hyandai 12GB DDR3 RAM (1x8GB, 1x4GB)
Procrastinator: Intel i5-5500U @ 2.2 GHz
Noisoundacreator: TURTLE BEACH PX4
Remembrerthing: WD 1TB Blue HDD
Flat-Colorful-Thing: Samsung SyncMaster 2323Y
See-A-Move-O: Tecknet M288

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, cferns1 said:

mine isn't working, using python 3.5.1

The only time I get the same error as you is with an empty "emplyeeInfo.txt" file.

 

Also, I'm using 3.5.0 to test. I doubt that's the issue.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, madknight3 said:

The only time I get that error is with an empty "emplyeeInfo.txt" file.

O.o I think the reason it was coming up was I hadn't saved the text file properly! whoops!!

Sorry about that, can you help me with the input though?

Every time I try and put one in it I keep getting errors

Spoiler

Future build:

http://uk.pcpartpicker.com/p/kGgNJx

 

Current build: (laptop)

 

Use the following style specs for your profile!
Specs:
Screeninator: Intel Graphics 5500
Stickaminator: Hyandai 12GB DDR3 RAM (1x8GB, 1x4GB)
Procrastinator: Intel i5-5500U @ 2.2 GHz
Noisoundacreator: TURTLE BEACH PX4
Remembrerthing: WD 1TB Blue HDD
Flat-Colorful-Thing: Samsung SyncMaster 2323Y
See-A-Move-O: Tecknet M288

 

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, Enderman said:

i dont have python installed to test myself, but i think its because this

 


for line in employeeInfo:
            employeeName,hireDate,birthdate=line.split(",")

 

is making local variables which cant be accessed by the elif statements outside the for loop

you need to make those things a variable at the start of of function welcomescreen and it should work

I would have thought that would be the case too but apparently it's allowed in Python. Still seems like a bad idea to me to do it though.

 

7 minutes ago, cferns1 said:

O.o I think the reason it was coming up was I hadn't saved the text file properly! whoops!!

Sorry about that, can you help me with the input though?

Every time I try and put one in it I keep getting errors

Can you post the code you're trying?

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, madknight3 said:

I would have thought that would be the case too but apparently it's allowed in Python.

 

Can you post the code you're trying?

def optionA():
    #enter employee details
    print ()
    print ("Employee Details")
    print ()
    employeeName=input("Enter the name of the employee you want to find")
    
    for line in employeeInfo:
            employeeName,hireDate,birthdate=line.split(",")
            

    print("The employee's name is:",employeeName)
    print("The employee was hired on:",hireDate)
    print("The employee's birthdate is:",birthdate)

def welcomeScreen():
    #welcome message
    print("#####################")
    print("Welcome to Caroline's Painting/Decorating Company Program")
    print("#####################")

    print()

    #options menu
    print(" A | Enter employee details")
    print(" B | Enter customer details")
    print(" C | Estimation")
    print(" D | Price Calculator")

    print()
    option=input("Please select an input option: ")

    if option=="A" or option=="a":
        optionA()


    elif option=="B" or option=="b":
        print("not implimented yet")
        welcomeScreen()


    elif option=="C" or option=="c":
        print("not implimented yet")
        welcomeScreen()

        
    elif option=="D" or option=="d":
        print("not implimented yet")
        welcomeScreen()


file=open("employeeInfo.txt","r")
employeeInfo=file.readlines()
file.close()
welcomeScreen()

I know the input isn't doing anything there but I tried changing the "for line in employeeInfo" to "for employeeName in employeeInfo" but did not work so I reverted it

Spoiler

Future build:

http://uk.pcpartpicker.com/p/kGgNJx

 

Current build: (laptop)

 

Use the following style specs for your profile!
Specs:
Screeninator: Intel Graphics 5500
Stickaminator: Hyandai 12GB DDR3 RAM (1x8GB, 1x4GB)
Procrastinator: Intel i5-5500U @ 2.2 GHz
Noisoundacreator: TURTLE BEACH PX4
Remembrerthing: WD 1TB Blue HDD
Flat-Colorful-Thing: Samsung SyncMaster 2323Y
See-A-Move-O: Tecknet M288

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, madknight3 said:

I would have thought that would be the case too but apparently it's allowed in Python. Still seems like a bad idea to me to do it though.

It's the normal way to write in Python since you can't declare a variable without initializing it like in other languages. Being forced to create a variable with some garbage value before a set of if statements that you know will change it is just a waste.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, cferns1 said:

I know the input isn't doing anything there but I tried changing the "for line in employeeInfo" to "for employeeName in employeeInfo" but did not work so I reverted it

When you're looping through the file you need to check if the employeeName is the employee you're looking for and if it is, break out of the loop. So first you'll need to change the name of one of your employeeName variables instead of overwriting the name you're looking for. Something like:

    searchName=input("Enter the name of the employee you want to find")
    
    for line in employeeInfo:
        employeeName,hireDate,birthdate=line.split(",")
        #check if employeeName is equal to searchName:
            #break if it is  

 

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×