未定义函数
Function not being defined
def winheight(height):
try:
height = int(raw_input('Enter the height of the window in metres: '))
except ValueError:
print 'Please enter an integer'
winheight(height)
winlength(length)
def winlength(length):
try:
length = int(raw_input('Enter the length of the window in metres: '))
except ValueError:
print 'Please enter an integer'
winlength(length)
pricing(height, length)
def pricing(height, length):
height_length = height * length
price = int(height_length) * 100
total = int(price) + 50
print int(total)
winheight(height)
这是我正在尝试的代码,用于完成 window 替换业务的 class 任务,每平方米的成本为 100 美元 + 最初的 50 美元
然而,每当我尝试 运行 这段代码时,我都会收到错误消息:
line 31, in
winheight(height) NameError: name 'height' is not defined
我需要全局定义高度和长度,以便我可以使用它们来支付最终费用,我不确定如何解决这个问题,任何帮助将不胜感激
height
确实没有定义。您在函数中定义的 height
变量是一个 local 变量。它在函数调用结束后停止存在。您可以 return 它,或使用全局变量(但不要使用全局变量)
def get_height():
while True:
try:
height = input()
except ValueError:
continue
else:
return height
并通过 height = get_height()
从您的主脚本调用它(在调用 pricing(height, length)
.
之前 length)
类似
一些其他注意事项:
- 再次尝试
input()
,使用 while 循环而不是递归调用函数(因为这会使之前的函数堆栈保持打开状态)
- 传递
height
变量并重新分配 height = input
没有意义。传递给 height 的值是一个副本,所以原始值不会改变。
您的代码应如下所示:
def winheight(): #define window-height-getting function
while True:
try:
height = int(raw_input("Enter the height of the window in metres: ")) #get user input for window height
return height
except ValueError: #catch improper inputs
print "Please enter an integer"
def winlength(): #define window-length-getting function
while True:
try:
length = int(raw_input("Enter the length of the window in metres: ")) #get user input for window length
return length
except ValueError: #catch improper inputs
print "Please enter an integer"
def pricing(height, length): #define price-calculating function
area = height * length #math to determine total price
price = area * 100
total = price + 50
return total
user_height = winheight() #get window height from user
user_length = winlength() #get window length from user
total_price = pricing(user_height, user_length) #calculate total price by passing user window height and length to the pricing function
print "The total price is: $" + str(total_price) #print result
def winheight(height):
try:
height = int(raw_input('Enter the height of the window in metres: '))
except ValueError:
print 'Please enter an integer'
winheight(height)
winlength(length)
def winlength(length):
try:
length = int(raw_input('Enter the length of the window in metres: '))
except ValueError:
print 'Please enter an integer'
winlength(length)
pricing(height, length)
def pricing(height, length):
height_length = height * length
price = int(height_length) * 100
total = int(price) + 50
print int(total)
winheight(height)
这是我正在尝试的代码,用于完成 window 替换业务的 class 任务,每平方米的成本为 100 美元 + 最初的 50 美元
然而,每当我尝试 运行 这段代码时,我都会收到错误消息:
line 31, in winheight(height) NameError: name 'height' is not defined
我需要全局定义高度和长度,以便我可以使用它们来支付最终费用,我不确定如何解决这个问题,任何帮助将不胜感激
height
确实没有定义。您在函数中定义的 height
变量是一个 local 变量。它在函数调用结束后停止存在。您可以 return 它,或使用全局变量(但不要使用全局变量)
def get_height():
while True:
try:
height = input()
except ValueError:
continue
else:
return height
并通过 height = get_height()
从您的主脚本调用它(在调用 pricing(height, length)
.
length)
类似
一些其他注意事项:
- 再次尝试
input()
,使用 while 循环而不是递归调用函数(因为这会使之前的函数堆栈保持打开状态) - 传递
height
变量并重新分配height = input
没有意义。传递给 height 的值是一个副本,所以原始值不会改变。
您的代码应如下所示:
def winheight(): #define window-height-getting function
while True:
try:
height = int(raw_input("Enter the height of the window in metres: ")) #get user input for window height
return height
except ValueError: #catch improper inputs
print "Please enter an integer"
def winlength(): #define window-length-getting function
while True:
try:
length = int(raw_input("Enter the length of the window in metres: ")) #get user input for window length
return length
except ValueError: #catch improper inputs
print "Please enter an integer"
def pricing(height, length): #define price-calculating function
area = height * length #math to determine total price
price = area * 100
total = price + 50
return total
user_height = winheight() #get window height from user
user_length = winlength() #get window length from user
total_price = pricing(user_height, user_length) #calculate total price by passing user window height and length to the pricing function
print "The total price is: $" + str(total_price) #print result