<p>我是python新手,我有一些值得怀疑的事情正在发生。我正试图找出我在这个问题上做错了什么:</p>
<p>本练习假设您已完成编程练习7“随机数文件”
作家编写另一个从文件中读取随机数的程序,显示
然后显示以下数据:
•总数
•从文件中读取的随机数</p>
<p>写入文件:</p>
<pre><code>import random
def main():
randomcount = int(input('How many numbers should i generate?'))
randomfile = open('randomnumber.txt', 'w')
total = 0
for numbers in range(randomcount):
number = random.randint(1,100)
total+= number
randomfile.write((str(number)) + '\n')
randomfile.write((str(total)) + '\n')
randomfile.close()
print('File updated')
main()
</code></pre>
<p>输出:</p>
<pre><code>How many numbers should i generate?5 (enter)
file updated - **Question 1**
file updated | ---- this is new.. while doing
file updated - trial and error this started
repeating. First 2 times then
after awhile 3 times.
Refreshed kernel and outputs
still does this
</code></pre>
<p>正在读取文件:<;——#<strong>主要问题</strong></p>
<pre><code>def main():
randomfile = open('randomnumber.txt','r')
contents = randomfile.readline()
while randomfile !='':
total = randomfile.readline()
contents = contents.rstrip('\n')
total = total.rstrip('\n')
print(contents)
contents = randomfile.readline()
print('total: ',total)
randomfile.close()
main()
</code></pre>
<p>输出:</p>
<pre><code>90 -
22 |
17 |--- Randomly generated
2 |
75 -
**Question 2**
<--- print('total: ', total) not showing up
-
|
|
|
. **Question 3**
. <--------- Space goes on forever like if its printing
. space. so much that theres a scroll bar just
. for empty space. if i try to scroll all the
| way to the bottom so that i can see if maybe
| its printing at the end i never reach the end
| of it because for some reason the program
- keeps adding more and more space.
</code></pre>