我查看了所有类似的问题,但找不到适合我情况的问题(或者也许有一个,但我是编程新手)。
我使用的 Python 版本是 2.7.4,我在程序中得到第 11 行的错误:NameError: global name 'opp' is not defined
我想制作一个计算地板尺寸的计算器。这是我的代码:
def oppervlakte():
global lengte
global br
global opp
lengte = raw_input("Voer de lengte in: ") # Put in the length
br = raw_input("Voer de breedte in: ") # Put in the width
opp = lengte * br # Calculates the dimension of the floor
return int(lengte), int(br) # Makes the variables lengte & br an integer
print opp
既然我现在得到了答案,我想和你分享,所以这里是:
def oppervlakte():
lengte = raw_input("Voer de lengte in: ") # Asks for the length
br = raw_input("Voer de breedte in: ") # Asks for the width
lengte = int(lengte) # String lengte --> int lengte
br = int(br) # String br --> int br
opp = lengte * br # Calculates the dimensions of the floor
return opp, lengte, br
opp, lengte, br = oppervlakte()
print "De oppervlakte is", opp # Prints the dimension