Python 检查随机数是否已经存在
我想生成随机数并将其写入.txt文件。 范围是:Python 检查随机数是否已经存在,python,Python,我想生成随机数并将其写入.txt文件。 范围是:str(random.randint(0,10)) 在生成随机数之前,我的代码应该首先检查.txt文件。在这个文本文件中已经写下了一些随机数。如果随机数已经存在,它应该生成一个新的随机数并将其添加到我的.txt文件中 randomTxt = './random.txt' def checkRandomNumberExists(value): with open(randomTxt, 'a+') as random: gen
str(random.randint(0,10))
在生成随机数之前,我的代码应该首先检查.txt文件。在这个文本文件中已经写下了一些随机数。如果随机数已经存在,它应该生成一个新的随机数并将其添加到我的.txt文件中
randomTxt = './random.txt'
def checkRandomNumberExists(value):
with open(randomTxt, 'a+') as random:
genRandom = str(random.randint(1,10))
if value in random:
random.write(genRandom)
random.write('\n')
我怀疑我走错了路。
谁能帮帮我吗。
提前感谢尝试在内部使用while循环:
randomTxt = './random.txt'
with open(randomTxt, 'a+') as file:
text = file.read()
genRandom = str(random.randint(1,10))
while genRandom in text:
genRandom = str(random.randint(1,10))
file.write(genRandom)
file.write('\n')
注意:请不要将文件和变量命名为内置名称(即随机),因为它可能会覆盖原始模块。尝试在内部使用while循环:
randomTxt = './random.txt'
with open(randomTxt, 'a+') as file:
text = file.read()
genRandom = str(random.randint(1,10))
while genRandom in text:
genRandom = str(random.randint(1,10))
file.write(genRandom)
file.write('\n')
注意:请不要将文件和变量命名为内置名称(即random),因为它可能会覆盖原始模块。我看不出有任何理由在函数中使用参数,因为您正在生成随机数,并且生成的随机数介于1-10之间,如果文本中添加了所有数字,如果它添加了除1,2,3…10以外的数字,请编辑您的问题并提及 下面的代码将检查文本文件中是否存在数字,如果不存在,则将在文本文件中添加数字,否则将打印数字已存在的消息 代码
import random
lines = []
num = random.randint(0,10)
f= open("random.txt","w+")
def checkRandomNumberExists():
with open("random.txt") as file:
for line in file:
line = line.strip()
lines.append(line)
if str(num) not in lines:
with open("random.txt", "a") as myfile:
myfile.write(str(num)+'\n')
print(str(num)+ ' is added in text file')
else:
print(str(num)+ ' is already exists in text file')
checkRandomNumberExists()
7 is added in text file
7 is already exists in text file
插入新值时输出
import random
lines = []
num = random.randint(0,10)
f= open("random.txt","w+")
def checkRandomNumberExists():
with open("random.txt") as file:
for line in file:
line = line.strip()
lines.append(line)
if str(num) not in lines:
with open("random.txt", "a") as myfile:
myfile.write(str(num)+'\n')
print(str(num)+ ' is added in text file')
else:
print(str(num)+ ' is already exists in text file')
checkRandomNumberExists()
7 is added in text file
7 is already exists in text file
当值已在文本文件中时输出
import random
lines = []
num = random.randint(0,10)
f= open("random.txt","w+")
def checkRandomNumberExists():
with open("random.txt") as file:
for line in file:
line = line.strip()
lines.append(line)
if str(num) not in lines:
with open("random.txt", "a") as myfile:
myfile.write(str(num)+'\n')
print(str(num)+ ' is added in text file')
else:
print(str(num)+ ' is already exists in text file')
checkRandomNumberExists()
7 is added in text file
7 is already exists in text file
我看不出有任何理由在函数中使用参数,因为您生成的是随机数,您生成的是1-10之间的随机数,如果文本中添加了所有数字,那么如果它添加了除1,2,3…10以外的数字,请编辑您的问题并提一下 下面的代码将检查文本文件中是否存在数字,如果不存在,则将在文本文件中添加数字,否则将打印数字已存在的消息 代码
import random
lines = []
num = random.randint(0,10)
f= open("random.txt","w+")
def checkRandomNumberExists():
with open("random.txt") as file:
for line in file:
line = line.strip()
lines.append(line)
if str(num) not in lines:
with open("random.txt", "a") as myfile:
myfile.write(str(num)+'\n')
print(str(num)+ ' is added in text file')
else:
print(str(num)+ ' is already exists in text file')
checkRandomNumberExists()
7 is added in text file
7 is already exists in text file
插入新值时输出
import random
lines = []
num = random.randint(0,10)
f= open("random.txt","w+")
def checkRandomNumberExists():
with open("random.txt") as file:
for line in file:
line = line.strip()
lines.append(line)
if str(num) not in lines:
with open("random.txt", "a") as myfile:
myfile.write(str(num)+'\n')
print(str(num)+ ' is added in text file')
else:
print(str(num)+ ' is already exists in text file')
checkRandomNumberExists()
7 is added in text file
7 is already exists in text file
当值已在文本文件中时输出
import random
lines = []
num = random.randint(0,10)
f= open("random.txt","w+")
def checkRandomNumberExists():
with open("random.txt") as file:
for line in file:
line = line.strip()
lines.append(line)
if str(num) not in lines:
with open("random.txt", "a") as myfile:
myfile.write(str(num)+'\n')
print(str(num)+ ' is added in text file')
else:
print(str(num)+ ' is already exists in text file')
checkRandomNumberExists()
7 is added in text file
7 is already exists in text file
当您同时使用
random
模块时,将打开的文件命名为“random”不是一个好主意。新随机数的生成速率是多少?每秒100次,每分钟一次,每天一次?这将影响代码的编写效率。您希望生成多少随机数?您可以实例化一个集合(从现有文件)并将生成的数字添加到集合中…之后,您可以检查生成的数字是否已在集合中。这是否回答了您的问题@molbdnilo更改了文件名当您同时使用random
模块时,将打开的文件命名为“random”不是一个好主意。新随机数的生成速率是多少?每秒100次,每分钟一次,每天一次?这将影响代码的编写效率。您希望生成多少随机数?您可以实例化一个集合(从现有文件)并将生成的数字添加到集合中…之后,您可以检查生成的数字是否已在集合中。这是否回答了您的问题@molbdnilo更改了文件name@m1711总是乐于助人@m1711总是乐于助人