Decided to play around with the FizzBuzz problem talked about in recitation. I expect this to stop @ 99 instead of 100, i = 0 while i < 100 : i += 1 print (i) and this to stop at 100 instead of 101, i = 0 while i <= 100 : i += 1 print (i) Can someone give me some insight here? Thanks!
well your codes are both valid...what do you need to know? other ways to write the same thing?
What I don't get is that int he first code set where i<100 the list prints out to 100. Shouldn't this stop @ 99 since 99 is less than 100 but 100 is equal to 100? In the second code set where i<=100 shouldn't this stop at 100 not 101 for the same reason?
oh yes, i = 0 while i < 100 : print (i) i += 1 try this
Why does adding the print statement make this correct?
well you practicaly print i before it goes +1 so: *You start with i=0 ,so you print 0. *Then it goes i=i+1 (its the same as i+=1). So you enter while loop again ,since i that is equal to 1 now is still less than 100. and you print 1. *then again i+=1 ,so now i =2, it passes the test and enters while..prints i (2). *adds 1 again etc.....when i becomes 99 ....99<100 is true so it prints i that is 99 and adds 1 to i. --> 100 *100 <100 is false so it does NOT enter the while loop and does NOT print 100. for the 2nd code 100<=100 is true so it enters while , prints 100 and goes 101. for 101 it does not enter while loop as it fails the test. hope that helps
Makes sens thanks
Join our real-time social learning platform and learn together with your friends!