Python 如何在另一个子例程中使用来自子例程的输入
Python how to use inputs from sub routines in another sub routine
我在 Python 中有一个问题要解决,即用户输入他们想要购买的商品数量等变量。我将其作为 def customer
,输出为 'how many widgets would you like',然后他们的输入乘以 10 英镑的价格。这一点工作正常。
我想将他们的输入带到另一个子例程中进行进一步的数学运算,例如税收和总计。是否有一些关键字供我研究以便我可以执行此操作?
到目前为止,这是我的代码:
def wall_1():
height = int(input("Enter the height in metres of wall 1 : "))
width = int(input("Enter the width in metres of wall 1 : "))
wall_1_price = height * width
price_all_walls(wall_1_price)
def wall_2():
height = int(input("Enter the height in metres of wall 2 : "))
width = int(input("Enter the width in metres of wall 2 : "))
wall_2_price = height * width
price_all_walls(wall_2_price)
def wall_3():
height = int(input("Enter the height in metres of wall 3 : "))
width = int(input("Enter the width in metres of wall 3 : "))
wall_3_price = height * width
price_all_walls(wall_3_price)
def wall_4():
height = int(input("Enter the height in metres of wall 4 : "))
width = int(input("Enter the width in metres of wall 4 : "))
wall_4_price = height * width
price_all_walls(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()
wall_2()
wall_3()
wall_4()
price_all_walls()
我不确定我是否正确理解了你的问题,但这可能会有所帮助。
def customer():
number_items = int(input("Enter the number of items to buy : "))
print("Widgets that you would like : £" + str(number_items * 10))
total_price = number_items * 10
further_maths(number_items, total_price) # Call to another subroutine further_maths with parameters number_items and total_price
def further_maths(number_items, total_price): # Subroutine definition
print("The total number of items in another subroutine : " + str(number_items))
print("The total price in another subroutine : " + str(total_price))
if __name__ == "__main__":
customer()
在此代码中,子例程 customer() 中的参数被传递到另一个子例程 further_maths()。
此代码示例将解决您的问题。 price_all_walls 方法接受四个参数。在您的 wall_1()、wall_2()、wall_3() 和 wall_4() 方法中,您仅使用一个参数调用 price_all_walls() (墙的价格)。这将引发错误 "the function definition does not exist"。
当你定义一个函数时,一个函数原型与它相关联(尽管这个术语在 C 和 C++ 编程语言中最常用),其中包括方法的名称和类型签名(参数类型 -> 不适用)在 python、return 类型等)。当您调用方法 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),它将高度和宽度作为参数,returns 是墙的价格。这导致了模块化代码,也提供了可重用性。
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)))
我没有演示全局变量的使用。你可以阅读它 here
我希望这能回答你的问题。
我在 Python 中有一个问题要解决,即用户输入他们想要购买的商品数量等变量。我将其作为 def customer
,输出为 'how many widgets would you like',然后他们的输入乘以 10 英镑的价格。这一点工作正常。
我想将他们的输入带到另一个子例程中进行进一步的数学运算,例如税收和总计。是否有一些关键字供我研究以便我可以执行此操作?
到目前为止,这是我的代码:
def wall_1():
height = int(input("Enter the height in metres of wall 1 : "))
width = int(input("Enter the width in metres of wall 1 : "))
wall_1_price = height * width
price_all_walls(wall_1_price)
def wall_2():
height = int(input("Enter the height in metres of wall 2 : "))
width = int(input("Enter the width in metres of wall 2 : "))
wall_2_price = height * width
price_all_walls(wall_2_price)
def wall_3():
height = int(input("Enter the height in metres of wall 3 : "))
width = int(input("Enter the width in metres of wall 3 : "))
wall_3_price = height * width
price_all_walls(wall_3_price)
def wall_4():
height = int(input("Enter the height in metres of wall 4 : "))
width = int(input("Enter the width in metres of wall 4 : "))
wall_4_price = height * width
price_all_walls(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()
wall_2()
wall_3()
wall_4()
price_all_walls()
我不确定我是否正确理解了你的问题,但这可能会有所帮助。
def customer():
number_items = int(input("Enter the number of items to buy : "))
print("Widgets that you would like : £" + str(number_items * 10))
total_price = number_items * 10
further_maths(number_items, total_price) # Call to another subroutine further_maths with parameters number_items and total_price
def further_maths(number_items, total_price): # Subroutine definition
print("The total number of items in another subroutine : " + str(number_items))
print("The total price in another subroutine : " + str(total_price))
if __name__ == "__main__":
customer()
在此代码中,子例程 customer() 中的参数被传递到另一个子例程 further_maths()。
此代码示例将解决您的问题。 price_all_walls 方法接受四个参数。在您的 wall_1()、wall_2()、wall_3() 和 wall_4() 方法中,您仅使用一个参数调用 price_all_walls() (墙的价格)。这将引发错误 "the function definition does not exist"。
当你定义一个函数时,一个函数原型与它相关联(尽管这个术语在 C 和 C++ 编程语言中最常用),其中包括方法的名称和类型签名(参数类型 -> 不适用)在 python、return 类型等)。当您调用方法 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),它将高度和宽度作为参数,returns 是墙的价格。这导致了模块化代码,也提供了可重用性。
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)))
我没有演示全局变量的使用。你可以阅读它 here
我希望这能回答你的问题。