help with python coding please

Associate
Joined
18 Aug 2004
Posts
1,886
Location
midlands
hi
been doing coding for few weeks at uni now and been going great until now :/
just cant work out what im doing wrong

Code:
def validated_input(prompt):
    value = input (prompt)
    while value <= 0:
        value = input("enter a number greater than zero:")
    return value

def wall_size():
    hight = validated_input ("how high is the wall? ")
    width = validated_input ("how wide is the wall? ")
    area = hight * width

def openings():
    op_hight = validated_input ("how high is the opening? ")
    op_width = validated_input ("how wide is the opening? ")
    opening = op_hight * op_width
    open_total = open_total + opening

 
    
open_total = 0
wall_size()
open_amount = input ("how many openings are there?")

for x in range(1,open_amount+1):
    openings()

print open_total

it is meant to work out how much paint is needed to paint a wall... but at the moment just working out how big the wall is
so it asks for the size of the wall then how many openings.. and calculates the openings
but it keeps coming up with:
Code:
how high is the wall? 20
how wide is the wall? 20
how many openings are there?1
how high is the opening? 2
how wide is the opening? 2
Error in Python code: Local variable 'open_total' referenced before assignment.

but open_total is assigned to 0 at the start of the code?

hope someone can work this out for me

rich

edit:
also if i try to print the value of area i get an error... is it just that you have to do something to get values out of functions?
 
Last edited:
You need to use the global keyword if you want to change variables in the global scope. So at the top of openings just write "global open_total". What its doing at the moment is trying to do open_total = open_total + opening in the local scope where open_total is not defined.
 
Back
Top Bottom