[Tutor] Print result of function doesn't work

Alan Gauld alan.gauld at yahoo.co.uk
Sun Jul 21 04:24:01 EDT 2024


On 20/07/2024 17:20, Torbjorn Svensson Diaz via Tutor wrote:

> print("What is the length of the first leg of the triangle?")
> leg1 = input()

You do know that you can put the prompt inside the input()?

leg1 = input("What is the length of the first leg of the triangle?")


> print("What is the length of the second leg of the triangle?")
> leg2 = input()
> 
> def hypotenuse(leg1, leg2):
>      legs = leg1**2 + leg2**2
>      result = math.sqrt(legs)
>      return result
> 
> x = hypotenuse

Here you assign x to the function hypotenuse. But you do not yet call
the function. Similar in Lisp to doing

(setq x hypotenuse)


> print(x)

So this will print the function object

> When I run it in Thonny it says as follows

> What is the length of the first leg of the triangle?
> 3
> What is the lenght of the second leg of the triangle?
> 4
> <function hypotenuse at 0x7fa05d5a1940>

Just as shown here.

> terminal but when I use interactive mode I have no difficulties.

Cut n paste your interactive session.
I suspect the line where you assign x looks different.

> The same program in Scheme is
> 
> (define (hypotenuse leg1 leg2) (sqrt (+ (expt leg1 2) (expt leg2 2))))
> (hypotenuse 3 4)

Notice that you do not assign the result of the function call, so its
not "the same"

Also you are not simply evaluating hypotenuse, that would be

(hypotenuse)

Can you see what's missing?

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos





More information about the Tutor mailing list