The print
, by default, writes to sys.stdout
(which in turn will display the message in the terminal where you are running Python). That is, it does not write to the file (unless you indicate, for example: print(mensagem, file=arquivo)
writes in the archive - whereas arquivo
was obtained by open
).
In your case, what is being written in the file is only what is passed to arquivo.write
. And since you only passed the number and nothing else, this is what will be written in the file. No print
you printed the number followed by a dot, but as already said, this is not being written in the file, but "on screen" (in the terminal where you run Python).
If you want to write all the numbers in the file, and between them you must have a ;
then do so:
with open('Crescente.txt', 'w') as arquivo:
arquivo.write(';'.join(str(n) for n in range(1, 101)))
The use of with
is interesting because it already closes the file at the end of the run.
Then I use join
to join the numbers, using the ;
as the character that will be between them. This way, the contents of the file will be:
1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;29;30;31;32;33;34;35;36;37;38;39;40;41;42;43;44;45;46;47;48;49;50;51;52;53;54;55;56;57;58;59;60;61;62;63;64;65;66;67;68;69;70;71;72;73;74;75;76;77;78;79;80;81;82;83;84;85;86;87;88;89;90;91;92;93;94;95;96;97;98;99;100
As it is an exercise, and these exercises usually have artificial limitations like "you can’t use [ready function of language]", may not be able to use join
. In this case, you could make one loop simple:
with open('Crescente.txt', 'w') as arquivo:
for n in range(1, 100): # vou até o penúltimo número
arquivo.write(f'{n};')
arquivo.write('100')
I used a f-string to print the number followed by ;
. This feature is available from Python 3.6, but if you are using a previous version, you can switch to:
arquivo.write('{};'.format(n))
The detail is that in for
I went from 1 to 99 (the range(1, 100)
includes numbers 1 to 99). That is, the for
writ 1;2;3;....99;
.
If I went up to 100, one ;
would be placed at the end, after the 100. To avoid this, I go up to 99 and at the end (outside the loop) write the 100 directly.
If you want to make the algorithm more "generic":
inicio = 1
fim = 100
# com join
with open('Crescente.txt', 'w') as arquivo:
arquivo.write(';'.join(str(n) for n in range(inicio, fim + 1)))
# com loop
with open('Crescente.txt', 'w') as arquivo:
for n in range(inicio, fim): # vou até o penúltimo número
arquivo.write(f'{n};')
arquivo.write(str(fim))
Yes, with
arquivo.write
you define what will be written in the file and you put only the numbers; already with theprint
you set what will be displayed in the terminal and you placed the numbers with a dot at the end. What you described matches the code you wrote. What was the expected result?– Woss
Igor, if one of the answers solved your problem, you can choose the one you solved best (only one of them) and accept it, see here how and why to do it. It is not mandatory, but it is a good practice of the site, to indicate to future visitors that it solved the problem. And when I get 15 points, you can also vote in all the answers you found useful.
– hkotsubo