Trying to code this, seems like it should work, not sure why it doesn't? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
0

Trying to code this, seems like it should work, not sure why it doesn't?

number = input() Length = len(number) final = 0 while length > 0: final = final + number[length] length = length - 1 print(final) The output should be the total of the input entered. Sample input 123 Expected output 6 1+2+3=6 I'm thinking I have the n[l] wrong somehow or that it can't be used that way. Any help would be appreciated. I originally hand n as the variable name for number and forgot to edit an n variable to number when I posted it. Tried to make it easier to read.

27th Apr 2021, 7:23 PM
Papalitzy
Papalitzy - avatar
9 Respuestas
+ 1
Papalitzy That's basically how it is meant to be accomplished for this task. You could just use 1 variable though. Try just using total and delete holder. Then use; total += n % 10 instead of holder = n % 10 and delete the line; total = holder + total
28th Apr 2021, 7:49 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
I was able to get it to work after playing around with it. Thank you guys for the help. I was trying to solve it a specific way for the practice or I would have used some of the examples given. This is how I solved it: n = int(input()) holder = 0 total = 0 while n > 0: holder = n % 10 n = n // 10 total = holder + total print(total) If there is a way to clean this code up while sticking to the intended practice of using loops I would appreciate it. Thank you again for all the help.
28th Apr 2021, 7:32 PM
Papalitzy
Papalitzy - avatar
+ 1
iTech I believe that the OP is using a while loop for the summing digits project which is in the while loop lesson section. So, it makes perfect sense to use one. Also, by your logic there is no need to use a for loop 😉, or even a loop at all 😉😁. print(sum(map(int, list(input()))))
27th Apr 2021, 8:33 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Papalitzy The object is to do it mathematically using the modulus operator and floor division to get the number in the ones place and add it to the total then remove the number in the ones place using floor division and repeat. You don't need the length from the given code anymore.
27th Apr 2021, 9:27 PM
ChaoticDawg
ChaoticDawg - avatar
0
You aren't setting a value to n. You should change n to number i think.
27th Apr 2021, 7:45 PM
Jerry Hobby
Jerry Hobby - avatar
0
n = input() final = 0 for i in n: final += int(i) print(final) # first, no need to use while loop # second, you forgot that any input is by default a string so you have to convert it to int in order to sum them
27th Apr 2021, 7:56 PM
iTech
iTech - avatar
0
ITech, you're correct in what lesson I'm working on. I'm not looking for a straight answer though. I'm hoping to figure out my code the way it stands. If I can understand why it's not working I feel as though I can fix it.
27th Apr 2021, 9:07 PM
Papalitzy
Papalitzy - avatar
0
ChaoticDawg thank you veeeeery much for the info! I never saw that map built-in function! this is fantastic wow 🤣🤣🤣 here we go again, another pretentious who recently learned how to print("hello world") wanna teach his master a built-in function, its getting ridiculously ridiculous!
27th Apr 2021, 10:09 PM
iTech
iTech - avatar
0
Papalitzy since its probably an assessment and you are forced to use while loop, I see 2 erros in your code: first you are trying to sum str, I already mentioned, input default type is str. second, your while loop iterator is wrong. Please learn more about while loop iterator variable. third, to sum all digits, while loop need to finish the iteration by arriving to the last element of the list which should be created at the input. last, in order to sum list elements, we use the while iterator variable which serves as index, hence list[iterator] means an element. I also converted it to int so we could sum them all. I hope you got the point. final = 0 # result num = 0 # counter which will serve as while loop iterator numbers = list(input()) # convert input to list while(num < len(numbers)): final += int(numbers[num]) # final + list element num += 1 # counter incremented by one, if it reach len(numbers), while loop stops print(final)
27th Apr 2021, 10:43 PM
iTech
iTech - avatar