Python Forum
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help in hoemwork
#11
Tried what?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#12
recursia..
never mind
Reply
#13
Look, this is your homework. We will help you to do it but we are not going to write it for you.
You're saying you tried recursion.
Post the code to see why is not working.
A simple example:
def factorial(n):
    if n == 1:
        return 1
    else:
        return n * factorial(n-1)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#14
You don't need recursion. I just showed you how to modify part of your program, you have to put that back into the while loop you already had. So yes, one pass through the code I showed you takes 74 to 11. Then the while loop runs through it again and 11 becomes 2. Then the while loop stops and returns the 2 you want.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#15
I recently just picked up python so I may not have the best answer...I have already solved it though.

My code has 2 functions. One function to count how many digits are in the number and another function that adds the digits in a number together.
I used a FOR loop to iterate through the number , adding each individual digit to a list. After that I made a variable to hold the sum of the numbers in the list.
My digit counting function counts the digits of the "final answer" , I then used a IF/ELSE statement to determine whether the digits could be added together further. If not return the "final answer" if yes then repeat.
Reply
#16
(Apr-09-2017, 06:38 AM)wavic Wrote: Do recursion.

Evil man Cool
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020