0

Cell Growth Explanation

Ended up having to search for the answer on this one. I'm sure there's more than one answer but this works, could someone please walk me through the while loop so I can get a better grasp on what's happening? Thanks # take the initial cell population as input cells = int(input()) # take the number of days as input days = int(input()) # initialize the day counter counter = 1 #complete the while loop while counter < days + 1: cells = cells * 2 # Daily message print("Day " + str(counter) + ": " +str(cells)) counter = counter + 1

24th Jul 2025, 8:46 AM
Ray VM
Ray VM - avatar
6 ответов
+ 2
Instead of counter = 1 and while counter < days + 1: you could also write: counter = 0 while counter < days: Test both versions with the same input. The result is equivalent. We only shift the counting by one, but that doesn't affect the "cells".
24th Jul 2025, 10:14 AM
Lisa
Lisa - avatar
+ 1
You have a very good code! In your code, the while loop will run until the counter value is greater than days. For example, if days = 9 and cells = 2, the while loop will run 9 times. Eventually, cells will become 512.
24th Jul 2025, 10:06 AM
Mila
Mila - avatar
0
Hi, The comments and code here are pretty self-explanatory. If you do not understand how this code works, you probably lack an understanding of python syntax. I suggest revising relevant lessons.
24th Jul 2025, 8:54 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
0
That much is obvious, and I have done so, but I'm still asking someone to walk me through it. Thanks It's really just this segment or perhaps I'm overthinking it: counter = 1 while counter < days + 1: The counter starts as 'counter = 1', why not 0? Or is a minimum of 1 day expected? Then, while counter is less than days, if this could be 0, still add 1? I initially thought a for loop would work better here, but that's not the question lol.
24th Jul 2025, 9:41 AM
Ray VM
Ray VM - avatar
0
Thanks Mila, it literally just clicked when you said "..the while loop will run until the counter value is greater than days". I was concentrating on the 'counter less than days' part too much. Cheers
24th Jul 2025, 10:16 AM
Ray VM
Ray VM - avatar
0
True, that works too, it just results as 'Day 0' which doesn't qualify in the test option in Sololearn so comes up as failed, but yeah, cool. Thanks Lisa.
24th Jul 2025, 10:56 AM
Ray VM
Ray VM - avatar