Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested function problem
#1
Hi everyone, I created this nested function to solve this formula: sqrt(sin(x)*(cos(x)) but it doesn't work. Where am I wrong?
Thanks
chipx

this is the code:

Output:
[output]import math def f1(x): a = math.sqrt(b) def f2(x): b = math.sin(x)*c def f3(x): c = math.cos(x) return c return b return a f1(5)
[/output]
Reply
#2
An embedded function still needs to be called. f1() never calls f2() and f2() never calls f3().
I think there is an indentation error. f3() never returns a value. f2() returns c, which is a variable inside f3(). f1() has two returns and will never get to "return a". The code should look more like this:
def f1(x):
    def f2(x):
        def f3(x):
            return math.cos(x)
        return f3(x) * math.sin(x)
    return math.sqrt(f2(x))
Code like this it should always be accompanied by a disclaimer.

"This code is written for the purposes of learning about embedded functions. Any use of this code would be a big mistake."
ndc85430 likes this post
Reply
#3
(Oct-21-2020, 03:01 PM)deanhystad Wrote: An embedded function still needs to be called. f1() never calls f2() and f2() never calls f3().
I think there is an indentation error. f3() never returns a value. f2() returns c, which is a variable inside f3(). f1() has two returns and will never get to "return a". The code should look more like this:
def f1(x):
    def f2(x):
        def f3(x):
            return math.cos(x)
        return f3(x) * math.sin(x)
    return math.sqrt(f2(x))
Code like this it should always be accompanied by a disclaimer.

"This code is written for the purposes of learning about embedded functions. Any use of this code would be a big mistake."


Thank you! but now what function do I call? to get a result.
Reply
#4
In the example, f1 calls f2 (in the return statement), and f2 calls f3. To get your answer, call f1(value)
Reply
#5
I imported math tried to call f1 but it gives me error:

Output:
f1(5)
"ValueError: math domain error"
Reply
#6
You will get that trying to take the square root of a negative number. sin(5) is negative, cos(5) is positive. f2(5) returns a negative number and f3(5) crashes when it tries to calculate the square root.

You should probably add some code to catch that problem and provide a more meaningful error message.
bowlofred likes this post
Reply
#7
I tried changing "cos" to "log10" and now it works.
Thank you!

Output:
import math def f1(x): def f2(x): def f3(x): return math.log10(x) return f3(x) * math.sin(x) return math.sqrt(f2(x)) f1(3)0.25948286130604603
Reply
#8
Until someone calls f1(4).
Reply
#9
Assuming this is actually a proof of concept, that is fine, but as @deanhystad points out, you really should have some checks to make sure you are not trying to get the square root of a negative number.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  nested function return MHGhonaim 2 562 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  return vs. print in nested function example Mark17 4 1,674 Jan-04-2022, 06:02 PM
Last Post: jefsummers
  Problem with nested JSON Kalet 7 2,719 Dec-09-2021, 11:13 PM
Last Post: Gribouillis
  Exit function from nested function based on user input Turtle 5 2,858 Oct-10-2021, 12:55 AM
Last Post: Turtle
Question Stopping a parent function from a nested function? wallgraffiti 1 3,618 May-02-2021, 12:21 PM
Last Post: Gribouillis
  accessing a second level nested function varsh 3 2,435 Aug-13-2020, 06:57 AM
Last Post: varsh
  How to make this function general to create binary numbers? (many nested for loops) dospina 4 4,331 Jun-24-2020, 04:05 AM
Last Post: deanhystad
  Nested Recursive Function not Returning Etotheitau 2 2,217 May-09-2020, 06:09 PM
Last Post: Etotheitau
  Nested while loop problem + turtle DreamingInsanity 3 2,906 Jul-06-2019, 02:01 PM
Last Post: DreamingInsanity
  Nested for loop strange problem mcva 2 2,570 Mar-16-2019, 12:53 PM
Last Post: mcva

Forum Jump:

User Panel Messages

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