+ 1

whats wrong with this code??

Its around a week since i started learning python from javascript I know this is a stupid question but why does this print up to 40 should it have printed as long as y is less than 20 https://code.sololearn.com/cKxRim5Z1MsB/?ref=app

20th Apr 2018, 7:06 AM
_c0d£ Bit£_
_c0d£ Bit£_ - avatar
6 Réponses
+ 4
When y is 20, it will print 40 as you have asked to print y*2, not y, if you were trying to make it count up by twos it would look like this: def num(y): while y < 20: y+=2 print(y) num(2) I’m not sure if this fixes your problem
20th Apr 2018, 7:13 AM
James Pinder
+ 4
That's because you use print(y*2) which outputs the incremented y value multiplied by 2; 3 -> 6 4 -> 8 ... 20 -> 40
20th Apr 2018, 7:17 AM
Ipang
+ 3
It's because y*2 does not change the value of y
20th Apr 2018, 7:14 AM
Eligijus Silkartas
Eligijus Silkartas - avatar
+ 3
Thanks for the summary Eligijus
20th Apr 2018, 7:15 AM
James Pinder
+ 2
I think it is: Because you increased y before you printf(y*2) def num(y): while y < 20: print(2*y) y+=1 num(2)
20th Apr 2018, 7:28 AM
ThiemTD
ThiemTD - avatar
+ 1
Got it,Thanks you all :-)
20th Apr 2018, 10:15 AM
_c0d£ Bit£_
_c0d£ Bit£_ - avatar