Make a table test would help to understand better. But anyway, let’s see what happens:
Supposing it was typed primeiro_termo = 1
and razao = 2
. The other variables will be:
variable |
value |
accountant |
1 |
outworking |
1 |
total |
0 |
a_more |
10 |
Like a_mais
vale 10
, he enters the while a_mais != 0
.
The line total = total + a_mais
is executed, so we have:
variable |
value |
accountant |
1 |
outworking |
1 |
total |
10 |
a_more |
10 |
Then it prints the message "Result = ", and by the values above we have to contador
is less than total
and therefore enters the while contador <= total
.
When executing resultado += razao
and contador += 1
, we have:
variable |
value |
accountant |
2 |
outworking |
3 |
total |
10 |
a_more |
10 |
He prints the resultado
(that is 3
), and how contador
is still less than total
, he continues in the while contador <= total
.
This process is repeated several times. I will skip these steps because it will be repetitive - after all, it is a loop :-) - but when contador
come to 11
, he comes out of while contador <= total
. At this point, we will have:
variable |
value |
accountant |
11 |
outworking |
21 |
total |
10 |
a_more |
10 |
Then you read again a value for a_mais
. Let’s assume that now I type 1
. I mean, right now a_mais
will be 1 and so it continues on while a_mais != 0
. So he runs the line total = total + a_mais
, and then we’ll have:
variable |
value |
accountant |
11 |
outworking |
21 |
total |
11 |
a_more |
1 |
Notice that total
has been updated, and so the condition of the while contador <= total
continues to be satisfied. That’s why he executes this while
again. There is no such story of "while condition is executed as if the counter is zeroed". The contador
has not been zeroed, only that the total
has been updated and so the condition of the while
is still valid.
Just for the record that this code is not printing the first term. In the above example I typed primeiro_termo = 1
and razao = 2
and the first printed number is 3
, but if the first term is 1
, then you shouldn’t start with 1
?
Anyway, I understand that - probably - this is an exercise and such, but only to leave registered, you can do this using the resources of the language itself. If you want a sequence of numbers, use range
, which is done just for this. So you can already print and pick up the size more easily (no longer need the counter):
primeiro_termo = int(input('Primeiro termo: '))
razao = int(input('Razão: '))
quantidade = 10 # quantidade de termos que será impressa
total = 0
while True:
numeros = range(primeiro_termo, primeiro_termo + quantidade * razao, razao)
total += len(numeros)
print(*numeros, sep=' ', end=' ')
quantidade = int(input('\nQuantos termos a mais você quer mostrar?: \n'))
if quantidade == 0:
break # sai do while
# nova sequência começa do próximo termo (ou seja, o último a ser impresso + a razão da PA)
primeiro_termo = numeros[-1] + razao
print(f'Obrigado por jogar. Você consultou {total} termos.')
I get it. That’s right, it’s just a while exercise. Actually, I was going to start the first term with 1, but I don’t know how to do that either.
– Marcos Facini
@Marcosfacini To start from 1, just invert (add the ratio only after printing): https://ideone.com/gV3aZs
– hkotsubo