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.
10/26/2020 12:39:15 AMUmar Haque
12 AnswersNew Answer
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 + 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 lean about for loops.
sum = 0 for digit in str(643): sum += int(digit) print(sum) Try this, should be self-explanatory.
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
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
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! :>
Max Harder Can you plz Explain???
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.
I had allready written Umar Haque
丹ⓨㄩک廾 If you explain that to me it would be very helpful
Using while loop: (what we all are taught) num = 12345 sum = 0 while num: sum += num % 10 num //= 10 print ( sum )
Try: num=643 digits=str(num) sum=0 for i in range(len(digits)): sum=sum+int(digits[i]) print(sum)
丹ⓨㄩک廾 Can you plz write a code for me so I can understand