在我的代码中,我创建了一个随机数的文本文件。但我想知道是否可以生成多个文件,每次在 for 循环中使用不同范围的随机数。
我对如何实现这一点有一个想法,但我不确定如何调用我每次 testn 制作的文件,其中 n 是 n 的当前值。所以我会有文件:test1、test2 等等。
到目前为止,我的实现是:
numberOfFiles = 5 #set this to the number of files you want to make
for n in range(numberOfFiles):
newFile = open('testn.txt', 'w') #if possible I want to create a file each time and call it testn, where n is the current value of n.
x = []
for i in range(10): #number of lines in each file
x.append(random.randint(0,60 + (n * 10))
for val in range(len(x)):
newFile.write(str(x[val]) + "\n")
newFile.close()