user3758480
user3758480

Reputation: 67

Local variable referenced before assignment python 3.4.1

I am trying to write a program that calculates the cost of a paint job in a room with no windows or doors. I have tried to do this numerous ways. I even tried to follow a similar example I found, but I am at a loss at this point. I keep running into numerous errors. I initially wanted 5 user inputs and that didn't work, so I tried two and still cannot get a proper run. Any help would be great!

# This program will calculate how much money
# it will cost to paint the walls of a shed
# that is rectangular in shape.
# Ask user to enter the length of each wall.
# Ask the user to enter the height of the walls.
# Calculate the square feet.
# Divide square feet by 300 (1) gallon.

import math

def main():


    length = float(input('Enter the length of wall 1: '))
    #L2 = float(input('Enter the length of wall 2: '))
    #L3 = float(input('Enter the length of wall 3: '))
    #L4 = float(input('Enter the length of wall 4: '))
    height = float(input('Enter the height of the walls: '))
    print('ft:', format(ft, '.2f'))

    #print('The cost of the paint is $', format(cost, '.2f'))
    ft, sq_ft, gal, cost = paint_cost(length, height)


def paint_cost(length, height):
    ft = length * height
    sq_ft = ft * height
    gal = sq_ft / 300
    cost = gal * 40
    return ft, sq_ft, gal, cost


# the 40 is the cost of one gallon of paint.

main()


Enter the length of wall 1: 5
Enter the height of the walls: 5
Traceback (most recent call last):
  File "E:\paint.py", line 38, in <module>
    main()
  File "E:\paint.py", line 21, in main
    print('ft:', format(ft, '.2f'))
UnboundLocalError: local variable 'ft' referenced before assignment

This is the message I am receiving now the way it is.

Upvotes: 0

Views: 644

Answers (1)

user3780831
user3780831

Reputation: 70

I think you made a simple error. Notice that you are trying to "print('ft:', format(ft, '.f')) before ft is even defined. Under your #print('The cost of..') line is the first time that you are calling the function paint cost.

So try this. Move "print('ft:', format(ft, '.2f'))" Underneath "ft, sq_ft, gal, cost = paint_cost(length, height)"

And that might work for you.

Upvotes: 3

Related Questions