0

I can't get this code, please help!

I can't understand how this code works and why is the input is 42 the output is 2, if you can give me any help I'll be grateful n = int(input()) length = 0 while n > 0: n //= 10 length += 1 print(length)

29th Mar 2022, 1:17 PM
NĂ©stor Velandia
NĂ©stor Velandia - avatar
2 Answers
+ 2
n = 42 42>0 true n//=10 => 42//10=4 length+=1 => length=1 n = 4 4>0 true n//=10 => 4//10=0 length+=1 => length=2 n=0 0>0 flase print(length) =>output : 2 It about finding number of digits in a number hope it helps..
29th Mar 2022, 1:22 PM
Jayakrishna 🇼🇳
+ 2
Hello NĂ©stor Velandia while 42 > 0 -> True 42 // 10 = 4 length = 1 while 4 > 0 -> True 4 // 10 = 0 length = 2 while 0 > 0 -> False In general this code counts the number of digits. 42 -> 2 digits -> length = 2 312 -> 3 digits -> length = 3
29th Mar 2022, 1:24 PM
Denise Roßberg
Denise Roßberg - avatar