[Tutor] Print result of function doesn't work
dn
PythonList at DancesWithMice.info
Sun Jul 21 15:42:01 EDT 2024
On 22/07/24 02:36, Mats Wichmann wrote:
> On 7/21/24 02:28, dn via Tutor wrote:
>> On 21/07/24 04:20, Torbjorn Svensson Diaz via Tutor wrote:
>>> Hello, dear tutors!
>>>
>>> Here's my program.
>>>
>>>
>>> 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
>>> print(x)
>>>
>>>
>>> When I run it in Thonny it says as follows
>>>
>>>
>>> %Run hypotenuse.py
>>> 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>
>>
>> This err.msg tells you that x has been set as an alias (second name)
>> of the function "hypotenuse".
>>
>> To call the function (and have it return a result) requires a pair of
>> parentheses and provision of the two arguments (leg1 and leg2).
>>
>> https://docs.python.org/3/tutorial/controlflow.html#defining-functions
>>
>
>
> in addition, input() returns a string. you appear to intend to use them
> as integers, so you need to convert.
Thonny is an excellent choice of editor for getting started with Python,
writing code, and more importantly, solving problems like this!
At coding-time it will highlight syntax errors and such-like. However,
there are similarly constructive and valuable aids available during
execution-time.
The View -> Variables menu selection enables visibility of what values
(and data-types) are being processed by the computer (and the way it
understands things cf what we are thinking/hoping/praying...).
The Simple Debugger will step-through lines of code, and with the above,
shows when-and-how identifiers' values are changing, which 'side' of a
condition has been chosen, when looping will (not) occur, and in this
case, whether or not a function is actually being executed the way we want.
It will be worth taking five or ten minutes to explore some of Thonny's
features and to include them in your development methodology!
--
Regards,
=dn
More information about the Tutor
mailing list