我有两个变量num_of_plants
和num_of_plants2
,我想传递给total()
,但我收到以下错误:
num_of_plants is undefined
def outer_flowerbed(length,spacing):
square_area = length ** 2
side = length/2
side_ba= (side**2)+(side**2)
side_c=math.sqrt(side_ba)
fill_area = side_c**2
outer_bed = square_area - fill_area
outer_triangles = outer_bed/4
conversion = spacing / 12
num_of_plants = outer_triangles/conversion**2
print "number of plants in each triangular bed: ",int(num_of_plants)
return num_of_plants
def circle_area(length,spacing):
radius = length/4
a = radius**2 * math.pi
conversion = spacing/12
num_of_plants2 = a/conversion**2
print "number of plants the circle garden has: ",int(num_of_plants2)
return num_of_plants2
def total(a,b):
add = a*4 + b
print "Total plants: ",add
如果函数起作用,则返回两个变量所代表的值。因此,如果您只是将函数直接用作total()的参数,那么它应该可以工作。你知道吗
看起来你在努力寻找一个花园所需要的植物数量
所以这里有一个过度设计的解决方案(使用一个抽象基类和两个子类):
就像
需要注意的是,num \u of \u plants和num \u of \u plants2是函数的本地函数,不能在函数外调用。但是,既然返回的是值,为什么不将两个函数的结果相加呢。所以你的主要功能是:
相关问题 更多 >
编程相关推荐