Feb-02-2021, 01:43 PM
Reading your question a second time, I see that most likely I was wrong as to how the file looks like
I see that in your file numbers are on the same line.
In this case the problem is if not(str(k)) in numer:`
here
Note that you approach is prone to errors e.g. you have 12 in your file. the way you check, even if you fix the condition, will eleminate also 1 and 2 (although 2 is present also separately).
I see that in your file numbers are on the same line.
In this case the problem is if not(str(k)) in numer:`
here
not(str(k))
is False
always. And you condition is actually if False in numer:
- which is always evaluated False
Note that you approach is prone to errors e.g. you have 12 in your file. the way you check, even if you fix the condition, will eleminate also 1 and 2 (although 2 is present also separately).
>>> '1' in '2 4 9 12 8 3\n' Truefile:
Output:2 4 9 12 8 3
import random fname = "bjjj.txt" with open(fname) as f: numbers = f.readline().strip().split() # print the numbers in the file: print(numbers) for _ in range(15): # loop 15 times k = str(random.randint(1, 16)) if k not in numbers: print (f'{k} não EXISTE') numbers.append(k) else: print (f'{k} EXISTE') with open(fname, 'w') as f: # open file in write mode f.write(f'{" ".join(numbers)}\n')
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs