D
D
Danil Arkhipov2020-02-04 18:11:58
Python
Danil Arkhipov, 2020-02-04 18:11:58

How to randomly open text files in PYTHON'E without repetition?

There is a program, it should randomly open one text file and process information, then randomly open another without repetition, etc.
Problem: opens with repetitions.

import random
r=1
i=0
while r < 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[0])
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:
print("((CORRECT ANSWER:"+str(c)+"))\n ((YOUR ANSWER"+str(v)+"))" )
if v == c:
print("JOY")
i+= 1
else:
print("Wrong answer")
print("Correct answer:"+str(c))
if r == 5:
break
r += 1
print("Correct answer:"+ str(i))

((dana 17 years old, 2 days familiar with a python, do not throw too much slippers))

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Kuts, 2020-02-04
@fox_12

There are two ways at a glance:
- add the file name to the set, before opening - check if the given file is in the set, if it already is - open another file
- a better option - get a list of files, sort in random order, open in turn
Z.Y . there was no desire to delve into the given piece of the program, since it was inserted into the question without formatting.

A
Andrey, 2020-02-04
@anerev

import os
import random

path =  os.listdir('путь/к/папке с txt')

while path:
    file = random.choice(path)
    with open(file, 'r') as f:
        b = f.readlines()
    path.remove(file)

    #код

D
Dimonchik, 2020-02-04
@dimonchik2013

open in a row, and who asks for a rand - in the snout

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question