LOOPS | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

LOOPS

Guys I've been finding it really hard to understand this code. n = int(input()) length = 0 while n > 0: length += (n % 10) n //= 10 print(length)

4th Feb 2022, 10:49 AM
NWACHUKWU PRECIOUS CALEB
NWACHUKWU PRECIOUS CALEB - avatar
4 Answers
+ 3
The code adds each digit of number <n> to <length>. For example, if <n> was 12345, then digit 5, 4, 3, 2, 1 (in that order) will be added to <length>.
4th Feb 2022, 10:53 AM
Ipang
+ 2
It will print the sum of the digits of the number Like you say, 1234 It will give 1+2+3+4= 10
4th Feb 2022, 10:53 AM
JOKER
JOKER - avatar
+ 2
Ex: input is 234 234>0 true length = 0 +(234%10) = 0+4=4 n//=10 => 234//=10=> 23 23>0 true length = 4 +(23%10) = 4+3=7 n//=10 => 23//=10 => 2 2>0 true length = 7 +(2%10) = 7+2=9 n//=10 => 2//=10=> 0 0>0 false. print(length) #9
4th Feb 2022, 10:55 AM
Jayakrishna 🇮🇳
0
Thanks for everything guys, solo won't be the same with y'all
4th Feb 2022, 4:44 PM
NWACHUKWU PRECIOUS CALEB
NWACHUKWU PRECIOUS CALEB - avatar