1
I have the following code:
def verificaPrimo(num):
if int(num) < 2:
return False
else:
for n in range(2, int(num)):
if int(num) % int(n) == 0:
return False
return True
def obtemNumeros(nomeArquivo):
arquivo = open(nomeArquivo, "r")
linha = arquivo.readline()
numeros = linha.split()
for line in arquivo:
numeros += line.split()
arquivo.close()
return numeros
arq = open("resultPrimos.txt", "w")
nomeArquivo = "numeros.txt"
numeros = obtemNumeros(nomeArquivo)
for numero in numeros:
verificaPrimo(numero)
if verificaPrimo(numero) == True:
arq.write(numero)
arq.write("\n")
arq.close()
The checks are OK, generate the output file is OK too, write the prime numbers in that file is OK too.
My problem is this:
In my input file, which is the.txt file, its content is:
1 2 3 4 5 6 7 8 9 10
50 -13 13 11
1
2
3
The output I get in the resultPrimos.txt file is:
2
3
5
7
13
11
2
3
I need the exit to be as follows:
2 3 5 7
13 11
2
3
Could you help me?
Note: yes, on the line where there are no primes, you must print a blank line.