+ 2

Python Problem (Another one)

Bro literally I am doing great and progressing in python. I haven't bought the pro version yet but there is also some free challenges. So like the other challenges there is a challenge about while loop called "While loop changed". And I literally cant solve it. Giving the codes description down below. You need to change the code to calculate and output the sum of all digits of the input number. Sample Input 643 Sample Output 13 Explanation The sum of the digits of 643 is 6+4+3 = 13. Thats how the question looks like. Man give me the solution as well as the explanation. Thank you for helping me.

26th Oct 2020, 12:39 AM
Umar Haque
Umar Haque - avatar
14 odpowiedzi
+ 4
Dont worry its quite easy to understand :- 1) First break down the problem . In this case , you have to add the digits of a number which is given as an input So :- Input--->Add the digits of the input--->print the output The input and print part isnt really hard ,is it ? Lets see the adding digits part Approach - iterate through the number by converting to a string. ITERATE means seeking each element/portion one by one . Converting to a string , why is it even necessary ? It is because only some data types in python ARE ITERABLE . One such type is a string So num = str(input()) #converting to a string sum = 0 #originally the sum is 0. for i in num : # This is the iteration , assuming that you know about for loops . If you dont , I suggest you to learn about them. sum = sum + eval(i) #Now , after the iterations its time to give the output ! print(sum) If you still dont understand what I meant , dont worry all of it will become obvious once you learn about for loops.
27th Oct 2020, 6:59 AM
Shubhank Gupta
Shubhank Gupta - avatar
+ 7
sum = 0 for digit in str(643): sum += int(digit)        print(sum) Try this, should be self-explanatory.
26th Oct 2020, 12:53 AM
Max Harder
Max Harder - avatar
+ 3
It is really easy.. but i have to use longer syntax than for in loop.. So i'll help you with the while loop This is my code: #in while loop: inp = input() sum = 0 d = 0 while d < len(inp): sum += int(inp[d]) d += 1 print(sum) #in for in loop: inp = input() sum = 0 for d in inp: sum += int(d) print(sum) And that's from me.. Stay confidence and keep typing! :>
27th Oct 2020, 8:43 AM
El Roy Situmorang
+ 2
Using while loop: (what we all are taught) num = 12345 sum = 0 while num: sum += num % 10 num //= 10 print ( sum )
26th Oct 2020, 3:38 AM
abhinav
abhinav - avatar
+ 1
Ok simply change them into list using list function then iterate it and add them a = list("12345") print(eval("+".join(a))) use this simple logic
26th Oct 2020, 12:59 AM
Ayush Kumar
Ayush Kumar - avatar
+ 1
In order to get the digits, convert the integer to a string and then sum up the characters as integers with the help of a for-loop.
26th Oct 2020, 1:02 AM
Max Harder
Max Harder - avatar
+ 1
print(sum(int(ichar for char in input())) Input a number. Using input() creates a string. Iterate over the string, converting each character to an integer with int(). Use sum() to add them together. https://code.sololearn.com/cOWCAxOR6Nqa
26th Oct 2020, 2:17 AM
David Ashton
David Ashton - avatar
+ 1
a b h i n a v had it right with some minor changes necessary for it to pass sololearn's test cases. Try this: n = int(input()) length = 0 sum = 0 while n > 0: sum += (n % 10) n //= 10 length += 1 print(sum)
16th Jun 2021, 2:00 PM
Theo
Theo - avatar
0
Max Harder Can you plz Explain???
26th Oct 2020, 12:59 AM
Umar Haque
Umar Haque - avatar
0
I had allready written Umar Haque
26th Oct 2020, 1:04 AM
Ayush Kumar
Ayush Kumar - avatar
0
丹ⓨㄩک廾 If you explain that to me it would be very helpful
26th Oct 2020, 1:06 AM
Umar Haque
Umar Haque - avatar
0
Try: num=643 digits=str(num) sum=0 for i in range(len(digits)): sum=sum+int(digits[i]) print(sum)
27th Oct 2020, 9:00 AM
Nikhil Gupta
Nikhil Gupta - avatar
0
@Theo can you please explain that?
22nd Aug 2021, 4:52 PM
Laila Mahomed
Laila Mahomed - avatar
- 1
丹ⓨㄩک廾 Can you plz write a code for me so I can understand
26th Oct 2020, 1:01 AM
Umar Haque
Umar Haque - avatar