first return works, the second doesn't
I'm doing the following exercise:
A number chain is created by continuously added the square of the
digits of a number to form a new number until it has been seen
before.For example:
44 → 32 → 13 → 10 → 1 → 1
85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89
Therefore any chain that arrives at 1 or 89 will become stuck in an
endless loop.
What is most amazing is that EVERY starting number will eventually
arrive at 1 or 89.
How many starting numbers below ten million will arrive at 89?
And my code just now is:
def start () :
start_range = range(4)
res = map(getDigits, start_range)
print(list(res))
def getDigits (num) :
check = checkNum(num)
if check is False :
strNum = str(num)
numList = map(toInt, strNum)
squareList = map(getSquareOfDigits, numList)
sumOfSquares = sum(squareList)
getDigits(sumOfSquares)
else :
return check
def checkNum(num) :
if num == 0 or num == 1 :
print(type(num))
print(num)
return 0
elif num == 89 :
print(type(num))
print(num)
return 1
else :
return False
def toInt (char) :
return int(char)
def getSquareOfDigits (num) :
return num * num
start()
The output is
<class 'int'>
0
<class 'int'>
1
<class 'int'>
89
<class 'int'>
89
[0, 0, None, None]
And I don't understand why, if it goes correctly through the elif it doesn't return the '1' and add it correctly to the list.
python python-3.x
add a comment |
I'm doing the following exercise:
A number chain is created by continuously added the square of the
digits of a number to form a new number until it has been seen
before.For example:
44 → 32 → 13 → 10 → 1 → 1
85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89
Therefore any chain that arrives at 1 or 89 will become stuck in an
endless loop.
What is most amazing is that EVERY starting number will eventually
arrive at 1 or 89.
How many starting numbers below ten million will arrive at 89?
And my code just now is:
def start () :
start_range = range(4)
res = map(getDigits, start_range)
print(list(res))
def getDigits (num) :
check = checkNum(num)
if check is False :
strNum = str(num)
numList = map(toInt, strNum)
squareList = map(getSquareOfDigits, numList)
sumOfSquares = sum(squareList)
getDigits(sumOfSquares)
else :
return check
def checkNum(num) :
if num == 0 or num == 1 :
print(type(num))
print(num)
return 0
elif num == 89 :
print(type(num))
print(num)
return 1
else :
return False
def toInt (char) :
return int(char)
def getSquareOfDigits (num) :
return num * num
start()
The output is
<class 'int'>
0
<class 'int'>
1
<class 'int'>
89
<class 'int'>
89
[0, 0, None, None]
And I don't understand why, if it goes correctly through the elif it doesn't return the '1' and add it correctly to the list.
python python-3.x
Can you clarify how your result is different from what you were expecting? As you only printtype(num)you always get<class 'int'>. Perhaps you wantedprint (type(check))ingetDigitsinstead? That will print the occasional<class 'bool'>.
– usr2564301
Nov 16 '18 at 10:05
add a comment |
I'm doing the following exercise:
A number chain is created by continuously added the square of the
digits of a number to form a new number until it has been seen
before.For example:
44 → 32 → 13 → 10 → 1 → 1
85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89
Therefore any chain that arrives at 1 or 89 will become stuck in an
endless loop.
What is most amazing is that EVERY starting number will eventually
arrive at 1 or 89.
How many starting numbers below ten million will arrive at 89?
And my code just now is:
def start () :
start_range = range(4)
res = map(getDigits, start_range)
print(list(res))
def getDigits (num) :
check = checkNum(num)
if check is False :
strNum = str(num)
numList = map(toInt, strNum)
squareList = map(getSquareOfDigits, numList)
sumOfSquares = sum(squareList)
getDigits(sumOfSquares)
else :
return check
def checkNum(num) :
if num == 0 or num == 1 :
print(type(num))
print(num)
return 0
elif num == 89 :
print(type(num))
print(num)
return 1
else :
return False
def toInt (char) :
return int(char)
def getSquareOfDigits (num) :
return num * num
start()
The output is
<class 'int'>
0
<class 'int'>
1
<class 'int'>
89
<class 'int'>
89
[0, 0, None, None]
And I don't understand why, if it goes correctly through the elif it doesn't return the '1' and add it correctly to the list.
python python-3.x
I'm doing the following exercise:
A number chain is created by continuously added the square of the
digits of a number to form a new number until it has been seen
before.For example:
44 → 32 → 13 → 10 → 1 → 1
85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89
Therefore any chain that arrives at 1 or 89 will become stuck in an
endless loop.
What is most amazing is that EVERY starting number will eventually
arrive at 1 or 89.
How many starting numbers below ten million will arrive at 89?
And my code just now is:
def start () :
start_range = range(4)
res = map(getDigits, start_range)
print(list(res))
def getDigits (num) :
check = checkNum(num)
if check is False :
strNum = str(num)
numList = map(toInt, strNum)
squareList = map(getSquareOfDigits, numList)
sumOfSquares = sum(squareList)
getDigits(sumOfSquares)
else :
return check
def checkNum(num) :
if num == 0 or num == 1 :
print(type(num))
print(num)
return 0
elif num == 89 :
print(type(num))
print(num)
return 1
else :
return False
def toInt (char) :
return int(char)
def getSquareOfDigits (num) :
return num * num
start()
The output is
<class 'int'>
0
<class 'int'>
1
<class 'int'>
89
<class 'int'>
89
[0, 0, None, None]
And I don't understand why, if it goes correctly through the elif it doesn't return the '1' and add it correctly to the list.
python python-3.x
python python-3.x
edited Nov 17 '18 at 22:15
mkrieger1
4,72722133
4,72722133
asked Nov 16 '18 at 9:15
J. Doe IIJ. Doe II
1
1
Can you clarify how your result is different from what you were expecting? As you only printtype(num)you always get<class 'int'>. Perhaps you wantedprint (type(check))ingetDigitsinstead? That will print the occasional<class 'bool'>.
– usr2564301
Nov 16 '18 at 10:05
add a comment |
Can you clarify how your result is different from what you were expecting? As you only printtype(num)you always get<class 'int'>. Perhaps you wantedprint (type(check))ingetDigitsinstead? That will print the occasional<class 'bool'>.
– usr2564301
Nov 16 '18 at 10:05
Can you clarify how your result is different from what you were expecting? As you only print
type(num) you always get <class 'int'>. Perhaps you wanted print (type(check)) in getDigits instead? That will print the occasional <class 'bool'>.– usr2564301
Nov 16 '18 at 10:05
Can you clarify how your result is different from what you were expecting? As you only print
type(num) you always get <class 'int'>. Perhaps you wanted print (type(check)) in getDigits instead? That will print the occasional <class 'bool'>.– usr2564301
Nov 16 '18 at 10:05
add a comment |
1 Answer
1
active
oldest
votes
In recursive functions you must have return values in your recursive call
def getDigits (num) :
check = checkNum(num)
if check is False :
strNum = str(num)
numList = map(toInt, strNum)
squareList = map(getSquareOfDigits, numList)
sumOfSquares = sum(squareList)
---------> return getDigits(sumOfSquares)
else :
return check
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334716%2ffirst-return-works-the-second-doesnt%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In recursive functions you must have return values in your recursive call
def getDigits (num) :
check = checkNum(num)
if check is False :
strNum = str(num)
numList = map(toInt, strNum)
squareList = map(getSquareOfDigits, numList)
sumOfSquares = sum(squareList)
---------> return getDigits(sumOfSquares)
else :
return check
add a comment |
In recursive functions you must have return values in your recursive call
def getDigits (num) :
check = checkNum(num)
if check is False :
strNum = str(num)
numList = map(toInt, strNum)
squareList = map(getSquareOfDigits, numList)
sumOfSquares = sum(squareList)
---------> return getDigits(sumOfSquares)
else :
return check
add a comment |
In recursive functions you must have return values in your recursive call
def getDigits (num) :
check = checkNum(num)
if check is False :
strNum = str(num)
numList = map(toInt, strNum)
squareList = map(getSquareOfDigits, numList)
sumOfSquares = sum(squareList)
---------> return getDigits(sumOfSquares)
else :
return check
In recursive functions you must have return values in your recursive call
def getDigits (num) :
check = checkNum(num)
if check is False :
strNum = str(num)
numList = map(toInt, strNum)
squareList = map(getSquareOfDigits, numList)
sumOfSquares = sum(squareList)
---------> return getDigits(sumOfSquares)
else :
return check
answered Nov 17 '18 at 20:25
user10608741
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334716%2ffirst-return-works-the-second-doesnt%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you clarify how your result is different from what you were expecting? As you only print
type(num)you always get<class 'int'>. Perhaps you wantedprint (type(check))ingetDigitsinstead? That will print the occasional<class 'bool'>.– usr2564301
Nov 16 '18 at 10:05