[Tutor] Print result of function doesn't work
Torbjorn Svensson Diaz
torbjorn_svensson_diaz at yahoo.com
Sun Jul 21 12:06:45 EDT 2024
On 7/21/24 10:24 AM, Alan Gauld via Tutor wrote:
> 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?")
Yes, "3" as leg1 and "4" as leg2.
>
>> 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.
>>> import math
>>> def hypotenuse(leg1, leg2):
... legs = leg1**2 + leg2**2
... result = math.sqrt(legs)
... return result
...
>>> hypotenuse(3, 4)
5.0
>> 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"
Ok, but the following works as a Scheme script
(define (hypotenuse leg1 leg2) (sqrt (+ (expt leg1 2) (expt leg2 2))))
(display (hypotenuse 3 4))
What I mean is that I get the display to work in Scheme but I don't get
the print to work in Python.
> Also you are not simply evaluating hypotenuse, that would be
>
> (hypotenuse)
>
> Can you see what's missing?
The parameters of the function? Is that what you mean?
If i write like this
import math
print("What is the length of the first leg of the triangle?")
leg1 = input()
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(leg1, leg2)
print(x)
it still doesn't work.
More information about the Tutor
mailing list