My code is suppose to take the digits i type in my raw_input and sum up the number

example: if i type 123456789 then i will get 1+2+3+4+5+6+7+8+9 = 45 as an answer

but it will not stop there it will continue adding until the sum reaches a single digit
so it will also sum up 45 as in 4+5 = 9 and 9 will be the final answer since it reaches a single digit. then it count how many time i added the numbers together for example
123456789 i added once and got 45 and i added a second time and got 9 so the count is 2

if the first while loop it got my code to sum up correctly such as if i type 12345
it wiill do this 1+2+3+4+5 = 15 and 15 will be mysumofdigit

but i can't get mysumofdigit in this case for example 15 to run a while loop that can do 5+1 = 6 and i have no clue on how to get it to count how many times i sum my digits

this is my code

myinput = int(raw_input("Enter a Whole Number, "))

addNumber  = myinput 


sumofdigit2 =0

while addNumber>0:
    digit = addNumber %10 
    sumofdigit= sumofdigit+ digit
    addNumber = addNumber /10

while sumofdigit>=10 and addNumber == 0:
    sumofdigit = addNumber
    digit2 = addNumber%10
    sumofdigit2= sumofdigit2+ digit2
    addNumber = addNumber /10

in my second while code i tried to test out my value and printed sumdigit2 and i keep getting 0

Here is a mock code of what i am suppose to do i got the first while code in correctly but not my second

userInput = addNumber <-- Here you are just moving the user input into a different variable. This way you can use the user input for Multiplication as well

while addNumber > 10 <-- Just a while loop additionally, I fat fingered this. This should be > 0

... find digit <-- sample code would be digit = addNumber % 10. From you example digit == 9 and addNumber == 123456789. addNumber was never changed

... find sum of digits <-- sumOfDigits = sumOfdigits + digit. You have to ensure that sumOfdigits is set to zero outside the while loop.

... remove least significant digit <-- addNumber = addNumber / 10 .. this now would make addNumber == 12345678

while sum digits >= 10 and addNumber == 0 <-- This loop only runs if the summation of digits is greater than 10 and you have pulled the last digit from your "digit stream"
addNumber = sum of digits
sum of digits = 0
Count of summations +=

Edited by philipshoe: wrong code

5 Years
Discussion Span
Last Post by pyTony
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.