擅长:python、mysql、java
<p>我重组了我的代码,使之成为for循环,而不是while循环。您不需要rstrip转换为int的数字。将累加器放在输入文件部分而不是输出文件部分。Mae的代码清理,它的工作</p>
<p>随机输入</p>
<pre><code>def main():
randomcount = int(input('How many numbers should i generate?'))
randomfile = open('randomnumber.txt', 'w')
for numbers in range(1,randomcount + 1):
numbers = random.randint(1,100)
randomfile.write(str(numbers) + '\n')
randomfile.close()
randomfile = open('randomnumber.txt','r')
total = 0
for numbers in randomfile:
numbers = int(numbers)
total+= numbers
print(numbers)
print('total',total)
main()
</code></pre>