我正在嘗試使用該函式來詢問計算面積的寬度和長度。但它總是說錯誤。
# 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: ')
我已經定義了,但它仍然說錯誤。錯誤是...
Traceback (most recent call last):
File"main.py", line 29 in <module>
space(length, width)
NameError: name 'length' is not defined
uj5u.com熱心網友回復:
這很簡單,您正在呼叫space(length, width)但length尚未width宣告,您可以做的就是從空間中洗掉引數,留下這樣的空間,space()因為它在這個函式內部,您實際上分配了這個變數。
最終代碼如下所示:
# 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: ')
uj5u.com熱心網友回復:
問題是您試圖將未宣告的變數傳遞給函式
space(length, width)
當您呼叫此函式時,您正在傳遞引數長度和寬度,但它們尚未定義
我還想提一下,您不需要傳遞任何變數,因此不需要應用它。
我已經看到有人回答了這個問題,但我想補充一點,當您回傳變數時,它沒有被分配給任何東西
area = space()
這應該有望解決您的問題!
uj5u.com熱心網友回復:
您將值傳遞給尚未定義的函式
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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/420387.html
標籤:
