Python 27.2 Summing Digits | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Python 27.2 Summing Digits

Hello! I'm trying to find out how to do this practice (ID 27.2). I've tried many ways but I can't seem to understand how to do this one. The question is: this given program calculated and outputs the number of digits in a given number using a while loop. During each interaction, the loop used floor division to divide the given number by 10, thus dropping one digit. The process continues until th num et has no more digit (n>0) Sample input 643 Sample output 13 You need to change the code to calculate and output the sum of all digits of the input number” The code it gives you is: n = int(input()) length = 0 while n > 0: n //= 10 length += 1 print(length) I tried to do this, amongst other attempts, but I can’t seem to figure out how to get the result it wants. length = 0 while n > 0: n //= 10 d = int(n) total = sum(d %10) print(total) length += 1 print(length)

31st Dec 2020, 7:55 PM
Max
11 Answers
+ 5
Max show us your attempt as this appears incomplete and we have no idea as to what you have or haven't done. Please use the 8 rules for getting help from the community. Your question may be reviewed and most likely will be marked for deletion. Thanks and happy coding. https://www.sololearn.com/discuss/1316935/?ref=app
31st Dec 2020, 8:35 PM
BroFar
BroFar - avatar
+ 3
Hi Guys, I've been slaving over this for days. Here is my correct code: n = int(input()) length = 0 while n>0: length += n%10 n //= 10 print(length)
17th Aug 2021, 9:26 PM
Adam Carnell
Adam Carnell - avatar
31st Dec 2020, 9:12 PM
BroFar
BroFar - avatar
+ 1
Hey BroFar . Happy new year. I’ve updated the original question with the full text to help you out understanding the whole problem and the information it gives out
1st Jan 2021, 4:37 PM
Max
+ 1
Hey there, I'm also super stuck, went back as I thought there was something I was missing. But I still can't grasp it. Any help would be very appreciative!
22nd Jan 2021, 7:33 PM
Justyn
Justyn - avatar
+ 1
good to know, Max i thought i wasnt paying attention as well lol i like the challenging ones for sure but only if they cover it with the material... hopefully we can get this one sorted out! i think this code is on the right track if i can get it to work lol n = int(input()) length = 0 while n > 0: n //= 10 length += 1 for i in range(length): print(n + n) print(n) we need to put stuff after the length += 1 and i think this would do it if i can get it to work
23rd Jan 2021, 1:23 PM
Steve Barone
Steve Barone - avatar
+ 1
It didnt work for me, but i doscovered taht we have a new “ get help” option and Andrei helped us all by finding this: __ Hey there. I took a look at 27.2. Here’s what I had: list = [1, 2, 3, 4, 5, 6, 7, 8, 9] sum = 0 #your code goes here for n in list: sum += n print(sum) __ BroFar Steve Barone
18th Aug 2021, 12:10 AM
Max
0
First...multiply the number (27.2) by 10 and convert it to an int. edit...if you have more than one digit after the decimal point..do this mynum = 27.22 while int(mynum) != float(mynum): mynum *= 10
31st Dec 2020, 10:11 PM
rodwynnejones
rodwynnejones - avatar
0
Max have you figured this one out? im stuck on it as well! i am going to try so,e of the tips here.
22nd Jan 2021, 3:29 AM
Steve Barone
Steve Barone - avatar
0
Steve Barone hey steve! no not yet. I decided to start over from scratch. I clearly missed something in the curriculum since i dont get this one. :S
22nd Jan 2021, 3:58 AM
Max
0
Adam Carnell unfortunately i couldn’t get that to work. I spent days on this one too lol
19th Aug 2021, 2:12 AM
Steve Barone
Steve Barone - avatar