'while' and 'for' count corn differently. How can I make both programs count the same? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 1

'while' and 'for' count corn differently. How can I make both programs count the same?

I wrote two codes based on the legend about the creator of chess. One using 'while' and the other using 'for'. I don‚Äôt understand how to make it count correctly with ‚Äėwhile‚Äô too corn = 1 cell = 64 counter = 1 while counter <= cell: corn *= 2 print('cell ' + str(counter) + ': ' + str(corn)) counter += 1 #and for i in range(64): cell = i+1 corn = 2**i print('cell: ', cell, 'corn' , corn)

16th Apr 2024, 2:38 AM
–≠–ī—É–į—Ä–ī –†–į–ł—Ā–ĺ–≤–ł—á
–≠–ī—É–į—Ä–ī –†–į–ł—Ā–ĺ–≤–ł—á - avatar
4 Respuestas
+ 2
In the while loop, corn starts as 1, but gets multiplied before being printed, so it effectively starts as 2. In the for loop, it is created as 1 and then printed.
16th Apr 2024, 3:21 AM
Wilbur Jaywright
Wilbur Jaywright - avatar
16th Apr 2024, 8:02 AM
Rain
Rain - avatar
+ 1
where were my eyes?
16th Apr 2024, 11:19 AM
–≠–ī—É–į—Ä–ī –†–į–ł—Ā–ĺ–≤–ł—á
–≠–ī—É–į—Ä–ī –†–į–ł—Ā–ĺ–≤–ł—á - avatar
0
thanks, but I need to know how to implement it in a while loop so that the report starts with 1 cell - 1 corn, 2 cell - 2 corn, 3cell- 4 corn .....
16th Apr 2024, 4:57 AM
–≠–ī—É–į—Ä–ī –†–į–ł—Ā–ĺ–≤–ł—á
–≠–ī—É–į—Ä–ī –†–į–ł—Ā–ĺ–≤–ł—á - avatar