How to randomly open text files in PYTHON'E without repetition?
There is a program, it should open randomly one text file and process information, after randomly open another without repetitions, etc.
Problem: opens with repetitions.
r = 1
i = 0
while r & lt; 10:
print ("Job #" + str (r))
a = int (random.randint (10, 14))
f = open (str (a) + ". txt", encoding = "utf-8")
b = f.readlines (1255)
print (b )
print ("1)" + str (b [-4]))
print ("2)" + str (b [-3]))
print ("3)" + str (b [-2]))
print ("4)" + str (b [-1]))
v = input (print ("Enter the number of the correct answer:")) + "; \ n"
for c in b:
if -1! = c.find (";"):
print ("((CORRECT ANSWER:" + str (c) + ")) \ n ((YOUR ANSWER" + str (v) + "))")
if v == c:
print ("GOOD MAN")
i + = 1
print ("Wrong answer")
print ("Correct answer:" + str (c))
if r == 5:
r + = 1
print ("Correct answers:" + str (i))
((tribute to 17 years old, 2 days familiar with python, do not throw sneakers too much))Python Anonymous, Nov 1, 2020
There are two ways to go:
- add the file name to set, before opening - check if the file is in set, if it already exists - open another file
- a better option is to get a list of files, sort in random order, open one by one
ZY there was no desire to delve into the given piece of the program, since they inserted it into the question without formatting.Paige Marquez
path = os.listdir('путь/к/папке с txt')
file = random.choice(path)
with open(file, 'r') as f:
b = f.readlines()
open a row, and whoever asks for a rent - in the snoutAnonymous
To place the code, please use CodePen or similar tool. Thanks you!