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
6 odpowiedzi
+ 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".
+ 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.
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.
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.
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
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.