如何添加循环的 return 值
How to i add the return values of a loop
我是 python a 的新手,我需要添加循环的 return 值。该程序应该打开一个包含 1x31x6 格式尺寸的文件,对它们进行排序并进行一些数学运算。我相当确定我一切都正确,但我不知道如何将 return 值加在一起。这是我目前的代码。
def parseAndSort(string):
"""this function reads the input file, parses the values, and sorts the ints from smallest to largest"""
int_list = string.split('x')
nums = [int(x) for x in int_list]
nums.sort()
length = nums[0]
width = nums[1]
height = nums[2]
surface_area = (2 * length * width) + (2 * length * height) + (2 * width * height) + (length * width)
tape = (2 * length) + (2 * width) + (length * width * height)
return surface_area
def main():
file = input('Please Input Measurement File :')
try:
output = open(file, "r")
except FileNotFoundError:
print("Error, please input a dimension file.")
else:
for ref in output:
parseAndSort(ref)
output.close()
if __name__ == "__main__":
""" This is executed when run from the command line """
main()
我假设你的意思是你想要 运行 函数所有时间的 return 值的总和。您可以保留一个 运行 总和,并继续向其中添加每个 return 值。
def main():
sum = 0
file = input('Please Input Measurement File :')
try:
output = open(file, "r")
except FileNotFoundError:
print("Error, please input a dimension file.")
else:
for ref in output:
sum += parseAndSort(ref)
output.close()
print (sum)
我是 python a 的新手,我需要添加循环的 return 值。该程序应该打开一个包含 1x31x6 格式尺寸的文件,对它们进行排序并进行一些数学运算。我相当确定我一切都正确,但我不知道如何将 return 值加在一起。这是我目前的代码。
def parseAndSort(string):
"""this function reads the input file, parses the values, and sorts the ints from smallest to largest"""
int_list = string.split('x')
nums = [int(x) for x in int_list]
nums.sort()
length = nums[0]
width = nums[1]
height = nums[2]
surface_area = (2 * length * width) + (2 * length * height) + (2 * width * height) + (length * width)
tape = (2 * length) + (2 * width) + (length * width * height)
return surface_area
def main():
file = input('Please Input Measurement File :')
try:
output = open(file, "r")
except FileNotFoundError:
print("Error, please input a dimension file.")
else:
for ref in output:
parseAndSort(ref)
output.close()
if __name__ == "__main__":
""" This is executed when run from the command line """
main()
我假设你的意思是你想要 运行 函数所有时间的 return 值的总和。您可以保留一个 运行 总和,并继续向其中添加每个 return 值。
def main():
sum = 0
file = input('Please Input Measurement File :')
try:
output = open(file, "r")
except FileNotFoundError:
print("Error, please input a dimension file.")
else:
for ref in output:
sum += parseAndSort(ref)
output.close()
print (sum)