此代码示例将解决您的问题。price_all_walls 方法接受四个参数。在您的 wall_1()、wall_2()、wall_3() 和 wall_4() 方法中,您调用 price_all_walls() 时只有一个参数(墙的价格)。这将引发错误“函数定义不存在”。
当你定义一个函数时,一个函数原型与它相关联(尽管这个术语在 C 和 C++ 编程语言中最常用),其中包括方法的名称和类型签名(参数类型 -> 在 python 中不适用,返回类型ETC。)。当您调用 price_all_walls() 方法时,应该使用四个参数调用它,因此您的代码可以修改如下:
def wall_1():
height = int(input("Enter the height in meters of wall 1 :"))
width = int(input("Enter the width in meters of wall 1:"))
wall_1_price = height * width
wall_2(wall_1_price)
def wall_2(wall_1_price):
height = int(input("Enter the height in meters of wall 2:"))
width = int(input("Enter the width in meters of wall 2:"))
wall_2_price = height * width
wall_3(wall_1_price, wall_2_price)
def wall_3(wall_1_price, wall_2_price)
height = int(input("Enter the height in meters of wall 3:"))
width = int(input("Enter the width in meters of wall 3:"))
wall_3_price = height * width
wall_4(wall_1_price, wall_2_price, wall_3_price)
def wall_4(wall_1_price, wall_2_price, wall_3_price):
height = int(input("Enter the height in meters of wall 4:"))
width = int(input("Enter the width in meters of wall 4:"))
wall_4_price = height * width
price_all_walls(wall_1_price, wall_2_price, wall_3_price, wall_4_price)
def price_all_walls(wall_1_price, wall_2_price, wall_3_price, wall_4_price):
print("The total price so far is : " + str(wall_1_price + wall_2_price + wall_3_price + wall_4_price))
if __name__=="__main__":
wall_1()
尽管这是一种非常低效的方法(没有好的程序员会建议这样做)。这个例子很好地解释了手头的问题。
如果您想编写此问题的代码,我建议您使用全局变量或按照以下代码所示的方式进行:
def wall_1():
height = int(input("Enter the height of wall 1 :"))
width = int(input("Enter the width of wall 1 :"))
wall_1_price = height * width
return wall_1_price
def wall_2():
height = int(input("Enter the height of wall 2:"))
width = int(input("Enter the width of wall 2:"))
wall_2_price = height * width
return wall_1_price
def wall_3():
height = int(input("Enter the height of wall 3:"))
width = int(input("Enter the width of wall 3:"))
wall_3_price = height * width
return wall_3_price
def wall_4():
height = int(input("Enter the height of wall 4:"))
width = int(input("Enter the width of wall 4:"))
wall_4_price = height * width
return wall_4_price
def price_all_walls(wall_1_price, wall_2_price, wall_3_price, wall_4_price):
return wall_1_price + wall_2_price + wall_3_price + wall_4_price
if __name__=="__main__":
wall_1_price = wall_1()
wall_2_price = wall_2()
wall_3_price = wall_3()
wall_4_price = wall_4()
print("The total price of the walls is : " + str(price_all_walls(wall_1_price, wall_2_price, wall_3_price, wall_4_price)))
尽管有人会建议这样做的最佳方法如下。声明一个函数 wall_n(int, int),它将高度和宽度作为参数并返回墙的价格。这导致了模块化代码,并且还提供了可重用性。
def wall_n(height, width):
wall_n_price = height * width
return wall_n_price
def price_all_walls(prices):
total_price = 0
for price in prices:
total_price += price
return total_price
if __name__=="__main__":
number_walls = int(input("Enter the number of walls to build : "))
wall_prices = []
for i in range(number_walls):
height = int(input("Enter the height of wall " + str(i) + " : "))
width = int(input("Enter the width of wall " + str(i) + " : "))
wall_prices.append(wall_n(height, width))
print("The total price is : " + str(price_all_walls(wall_prices)))
我没有演示全局变量的使用。你可以在这里阅读
我希望这回答了你的问题。