How to fix this function? I've already defined it but it still won't work
I am trying to use the function for asking for the width and length, of something calculating the area. But it always says error.
# defining functions
def space(length, width):
""" This function will calculate the area of floor, that needs
flooring. """
# asking for variables
length =int(input('Please enter the length of the room in meters: '))
width =int(input('Please enter the width of the room in meters: '))
area=length*width
return area
print('Flooring for the Future ')
space(length, width)
print('Types of Flooring')
print(' Cost per sq.m.')
print('1. Low Pile Carpet $18.75')
print('2. Shag Rug $11.05')
print('3. Parquet $14.35')
print('4. Lineleum $10.40')
print('5. Hardwood $28.15')
print()
opt=input('Please select type of flooring: ')
I already defined but it still says error. The error is...
Traceback (most recent call last):
File"main.py", line 29 in <module>
space(length, width)
NameError: name 'length' is not defined
Solution 1:
This one is quite simple, you are calling space(length, width)
but length
nor width
have been declared, what you can do is just remove the arguments from space leaving it like this space()
because it is inside this function where you actually assign this variables.
The final code would look something like this:
# defining functions
def space():
""" This function will calculate the area of floor, that needs
flooring. """
# asking for variables
length =int(input('Please enter the length of the room in meters: '))
width =int(input('Please enter the width of the room in meters: '))
area=length*width
return area
print('Flooring for the Future ')
space()
print('Types of Flooring')
print(' Cost per sq.m.')
print('1. Low Pile Carpet $18.75')
print('2. Shag Rug $11.05')
print('3. Parquet $14.35')
print('4. Lineleum $10.40')
print('5. Hardwood $28.15')
print()
opt=input('Please select type of flooring: ')