【发布时间】:2013-02-16 06:45:33
【问题描述】:
说明:
- 编写一个程序,将一系列随机数写入文件。
- 每个随机数应在 1 到 100 的范围内。
- 应用程序应让用户指定文件将保存多少个随机数。
这是我所拥有的:
import random
afile = open("Random.txt", "w" )
for line in afile:
for i in range(input('How many random numbers?: ')):
line = random.randint(1, 100)
afile.write(line)
print(line)
afile.close()
print("\nReading the file now." )
afile = open("Random.txt", "r")
print(afile.read())
afile.close()
几个问题:
不是根据用户设置的范围在文件中写入随机数。
文件一旦打开就无法关闭。
读取文件时,什么都没有。
虽然我认为设置没问题,但它似乎总是在执行时卡住。
【问题讨论】: