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
CodePudding user response:
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: ')
CodePudding user response:
The problem is you are trying to pass variables that aren't declared, into a function
space(length, width)
When you call this function you are passing the parameters length and width yet they are not yet defined
I would also like to mention that you need not pass any variables, so it need not be applied.
I already see that someone has answered this, but I would like to add that when you return the variable, it is not being assigned to anything
area = space()
This should hopefully fix your problems!
CodePudding user response:
You passing values to function that are not yet defined
def space( ):
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
space()
