-1
Hello, I am beginner and am trying to create a program that generates a text file but each line being random (based on predefined phrase).
The code works and does not return errors at the prompt, but it only writes "None" in the file. And if repeat 3 times, appears 3 times None (Nonenonenone).
This is an example of the code:
import random
with open('texto.txt','a') as f:
print('Para finalizar, digite "fim"')
while True:
escolha1 = input("escolha uma letra: ")
if escolha1 == "fim":
break
escolha2 = input("escolha de novo: ")
texto1 = ["%s é uma letra diferente de %s"%(escolha1,escolha2),"os dois são diferentes","%s não parece com %s"%(escolha1,escolha2)]
texto2 = ["os dois são iguais"," são mesma coisa","escolheu a mesma coisa"]
if escolha1 != escolha2:
q = str(random.choice(texto1))
f.write(str(print('%s' % q)))
else:
w = str(random.choice(texto2))
f.write(str(print('%s' % w)))
continue
print("fim")
f.close()
I’ve tried it myself f = open("texto.txt", "a")
and with the f.write
straightforward (f.write(str(random.choice(texto)))
)
f.write(str(print('%s' % q)))
, what you are writing in the file is the return of the functionprint
, that will always beNone
. If you want the phrase to be written in the file, just do itf.write(q)
– Woss