我正在编写我的代码,然后按照Tony Gaddis的书“Begin Out with Python:Second Edition”获取我在MCC的COMS-170,出于某些原因我不断收到错误
Traceback (most recent call last):
File "C:/Users/tcarroll/Desktop/123.py", line 36, in <module>
main()
TypeError: main() missing 1 required positional argument: 'nums8'
这是我的代码:
def main(nums8):
inFile = open('nums8.txt', 'r')
num1 = int(inFile.readline())
num2 = int(inFile.readline())
num3 = int(inFile.readline())
num4 = int(inFile.readline())
num5 = int(inFile.readline())
num6 = int(inFile.readline())
num7 = int(inFile.readline())
num8 = int(inFile.readline())
num9 = int(inFile.readline())
num10 = int(inFile.readline())
num11 = int(inFile.readline())
num12 = int(inFile.readline())
num13 = int(inFile.readline())
num14 = int(inFile.readline())
num15 = int(inFile.readline())
num16 = int(inFile.readline())
num17 = int(inFile.readline())
num18 = int(inFile.readline())
num19 = int(inFile.readline())
num20 = int(inFile.readline())
num21 = int(inFile.readline())
num22 = int(inFile.readline())
inFile.close()
total = int(num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10 + num11 + num12 + num13 + num14 + num15 + num16 + num17 + num18 + num19 + num20 + num21 + num22)
print('The numbers are: ', num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12, num13, num14, num15, num16, num17, num18, num19, num20, num21, num22)
print('Their total is: ', total)
main()
书中的问题是: 5.数字之和 假设包含一系列整数的文件名为numbers.txt(对于此分配,我们实际上有一个我们下载和使用的文件)并存在于计算机的磁盘上。编写一个程序,读取存储在文件中的所有数字并计算它们的总数。
答案 0 :(得分:1)
只需在没有参数的情况下定义主函数:
def main():
inFile = open('nums8.txt', 'r')
此外,您还希望将文件读取代码放入此功能中:
def main(nums8):
inFile = open('nums8.txt', 'r')
num1 = int(inFile.readline())
num2 = int(inFile.readline())
num3 = int(inFile.readline())
num4 = int(inFile.readline())
num5 = int(inFile.readline())
num6 = int(inFile.readline())
num7 = int(inFile.readline())
num8 = int(inFile.readline())
num9 = int(inFile.readline())
num10 = int(inFile.readline())
num11 = int(inFile.readline())
num12 = int(inFile.readline())
num13 = int(inFile.readline())
num14 = int(inFile.readline())
num15 = int(inFile.readline())
num16 = int(inFile.readline())
num17 = int(inFile.readline())
num18 = int(inFile.readline())
num19 = int(inFile.readline())
num20 = int(inFile.readline())
num21 = int(inFile.readline())
num22 = int(inFile.readline())
inFile.close()
total = int(num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10 + num11 + num12 + num13 + num14 + num15 + num16 + num17 + num18 + num19 + num20 + num21 + num22)
print('The numbers are: ', num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12, num13, num14, num15, num16, num17, num18, num19, num20, num21, num22)
print('Their total is: ', total)
main()
另外,您可能希望将其写得更短:
def main():
with open('nums8.txt') as inFile:
nums = [int(line) for line in inFile]
total = sum(nums)
print('The numbers are: ', nums)
print('Their total is: ', total)
此示例与for循环,列表,列表推导,行式文件读取,上下文管理器(&#34;使用open(...)&#34; stuff)以及查找值的总和相关名单。这里的好处是它汇总了文件中的所有行,并不关心有多少行(零或十亿或更多)
快乐的谷歌搜索!