'while' and 'for' count corn differently. How can I make both programs count the same? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 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 Antworten
+ 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
+ 2
ЭЮуарЮ Đ Đ°ĐžŃĐŸĐČоч , Then print it before you multiply it.
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