From torbjorn_svensson_diaz at yahoo.com Sat Jul 20 12:20:11 2024 From: torbjorn_svensson_diaz at yahoo.com (Torbjorn Svensson Diaz) Date: Sat, 20 Jul 2024 18:20:11 +0200 Subject: [Tutor] Print result of function doesn't work References: Message-ID: 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 I have the same trouble when I run the program as a script in the terminal but when I use interactive mode I have no difficulties. The same program in Scheme is (define (hypotenuse leg1 leg2) (sqrt (+ (expt leg1 2) (expt leg2 2)))) (hypotenuse 3 4) and works wonders. Why doesn't my Python program run? What do I do wrong? Can someone help me out? Perhaps give me some pointers as to what I should learn? Thanks in advance and best regards, From alan.gauld at yahoo.co.uk Sun Jul 21 04:24:01 2024 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 21 Jul 2024 09:24:01 +0100 Subject: [Tutor] Print result of function doesn't work In-Reply-To: References: Message-ID: 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 > 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 From PythonList at DancesWithMice.info Sun Jul 21 04:28:34 2024 From: PythonList at DancesWithMice.info (dn) Date: Sun, 21 Jul 2024 20:28:34 +1200 Subject: [Tutor] Print result of function doesn't work In-Reply-To: References: Message-ID: 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 > 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 -- Regards, =dn From mats at wichmann.us Sun Jul 21 10:36:39 2024 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 21 Jul 2024 08:36:39 -0600 Subject: [Tutor] Print result of function doesn't work In-Reply-To: References: Message-ID: <126dd07c-d3e2-430e-8e1d-5b9127e01ceb@wichmann.us> 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 >> > > 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. From PythonList at DancesWithMice.info Sun Jul 21 15:42:01 2024 From: PythonList at DancesWithMice.info (dn) Date: Mon, 22 Jul 2024 07:42:01 +1200 Subject: [Tutor] Print result of function doesn't work In-Reply-To: <126dd07c-d3e2-430e-8e1d-5b9127e01ceb@wichmann.us> References: <126dd07c-d3e2-430e-8e1d-5b9127e01ceb@wichmann.us> Message-ID: <25058c38-578f-4d32-8a66-c78147a1c9e3@DancesWithMice.info> 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 >>> >> >> 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 From torbjorn_svensson_diaz at yahoo.com Sun Jul 21 12:06:45 2024 From: torbjorn_svensson_diaz at yahoo.com (Torbjorn Svensson Diaz) Date: Sun, 21 Jul 2024 18:06:45 +0200 Subject: [Tutor] Print result of function doesn't work In-Reply-To: References: Message-ID: 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 >> > 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. From Richard at Damon-Family.org Sun Jul 21 20:09:27 2024 From: Richard at Damon-Family.org (Richard Damon) Date: Sun, 21 Jul 2024 20:09:27 -0400 Subject: [Tutor] Print result of function doesn't work In-Reply-To: References: Message-ID: On 7/21/24 12:06 PM, Torbjorn Svensson Diaz via Tutor wrote: > > 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 >>> >> 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. You might want to look at the error message you get. Hint, look at the type that input returns, and the sort of type you want to give to your function. -- Richard Damon From alan.gauld at yahoo.co.uk Sun Jul 21 20:37:43 2024 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 22 Jul 2024 01:37:43 +0100 Subject: [Tutor] Print result of function doesn't work In-Reply-To: References: Message-ID: On 21/07/2024 17:06, Torbjorn Svensson Diaz via Tutor wrote: >> 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. That would be thevalues you input as a result of the prompt. I was merely pointing out that you don;t need a separate print() line, you can put the prompt string inside the input() call. >>> def hypotenuse(leg1, leg2): >>> ??? legs = leg1**2 + leg2**2 >>> ??? result = math.sqrt(legs) >>> ??? return result >>> >>> x = hypotenuse >> 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 notice you are a) not assigning the result to x before printing. and b) providing parameters to hypotenuse() > (define (hypotenuse leg1 leg2) (sqrt (+ (expt leg1 2) (expt leg2 2)))) > (display (hypotenuse 3 4)) Yes, but again there is no intermediate assignment to x. The programs are not equivalent. >> (hypotenuse) >> >> Can you see what's missing? > > The parameters of the function? Is that what you mean? Exactly. The function name by itself evaluates to a function object(just as it would in Lisp/scheme). You must provide the parameters inside parens for Python to execute the function. In fact its the parens that cause the execution, you need them even if there are no arguments. > If i write like this > > import math > > print("What is the length of the first leg of the triangle?") > leg1 = input() input() reads a string from the user. You should convert it to an int/float before using it. > > print("What is the length of the second leg of the triangle?") > leg2 = input() > > def hypotenuse(leg1, leg2): > ??? legs = leg1**2 + leg2**2 This should give a type error because you are passing strings not ints(or floats). > ??? result = math.sqrt(legs) > ??? return result > > x = hypotenuse(leg1, leg2) > print(x) > > > it still doesn't work. If you fix the type errors it should be ok. -- 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 From threesomequarks at proton.me Sun Jul 21 22:44:16 2024 From: threesomequarks at proton.me (ThreeBlindQuarks) Date: Mon, 22 Jul 2024 02:44:16 +0000 Subject: [Tutor] Print result of function doesn't work In-Reply-To: References: Message-ID: This simple request has become too long. You can write a program like this fairly simply in any language without needing to start with a LISP Dialect and trying to sort of translate it into Python. The code offered has two parts and an amazing number of errors. But start over and think in Python. How does one define a function and specify it takes two variables? How does one check if the two variables are of an appropriate kind and if not, deal with it either by coercing them to the type you need or dealing with or propagating an error? Assuming the two arguments are reasonable, as in integers or floating point or some class you can do arithmetic with, how does one compute squares of numbers, add them together and take a positive square root? How does one return a value from a function, in this case probably a single floating point number? How do you test a function with some suite of tests and validate the results? On to your next part. It sounds like you want an interactive program that asks the user for two numbers for the length of the base and height of some right triangle and call the function to calculate the length of the hypotenuse and presumably print it out. The very first thing you need is to write out a description, perhaps your own way, but ideally consider things like what I am writing. You then need to research not just how to create a function, but what Python functionality you can use for the rest. How does one print things that a user can read? How does one read the answer? In what form is the answer? Is it characters or some form of number or maybe something else? What form does it need to be as an argument to the function? Realistically, there can be many possibilities as you are also writing the function! For example, here is my slightly odd implementation, as explained below: # CODE START import math def hype(base, height): return math.sqrt(float(base)**2 + float(height)**2) hype(input("Base: "), input("Height: ")) # CODE END What does this do? This being Python and not LISP, my function takes anything that can validly be coerced to a floating point and then uses it as changed to return a float result. Either or both inputs can be integers or floating point numbers but also text as in "5.3" and with some effort, it could probably be made to handle binary and octal and hexadecimal and fractional representations. For now, it does not check for errors as that would need thought and lots more code. All it does is hope converting to float works and apply the formula and return the result. But since it works on strings containing proper numbers, you can call it with a pair of input statements that can be seen in this transcript with examples and the text returned by input is converted: >>> hype(input("Base: "), input("Height: ")) Base: 3 Height: 4.0 5.0 >>> hype(input("Base: "), input("Height: ")) Base: 5.0e2 Height: 12.1e3 12110.326172320876 No, this is not what you should design but is an example of showing your design can be done often in many ways as long as the parts are built to work together. If you were sure the arguments would be exactly say integers, fine. If not sure, your code might repeatedly try to evaluate what kind of argument you got and try coercing it various ways to get a result you want. As an example, float() does not convert a string containing binary or hexadecimal or float. But doing something like this as an experiment: >>> int("0b111", 2) 7 >>> int("0b111", 8) Traceback (most recent call last): File "", line 1, in ValueError: invalid literal for int() with base 8: '0b111' The above could be used in a try clause to try various conversions and see what sticks and enlarge possible uses of the function a lot but for your purposes, is it worth the bother? My guess is your current needs are simple but you should handle what happens if the user enters no numbers or something invalid. So make a clear design and implement THAT. Sent with Proton Mail secure email. On Saturday, July 20th, 2024 at 12:20 PM, 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 = leg12 + leg22 > 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 > > > > > I have the same trouble when I run the program as a script in the > terminal but when I use interactive mode I have no difficulties. > > > The same program in Scheme is > > (define (hypotenuse leg1 leg2) (sqrt (+ (expt leg1 2) (expt leg2 2)))) > (hypotenuse 3 4) > > and works wonders. Why doesn't my Python program run? What do I do > wrong? Can someone help me out? Perhaps give me some pointers as to what > I should learn? > > Thanks in advance and best regards, > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From ahmadraufbd at gmail.com Mon Jul 22 00:11:37 2024 From: ahmadraufbd at gmail.com (rauf ahmad) Date: Mon, 22 Jul 2024 09:11:37 +0500 Subject: [Tutor] Print result of function doesn't work In-Reply-To: References: Message-ID: It's easy to deal with: x = hypotenuse # Here you didn't call the function, so call it by passing your parameters like: x = hypotenuse(leg1, leg2) print(x) # This will now result correctly Notice: you defined a function: def hypotenuse(leg1, leg2): Here, leg1 and leg2 are not the actual variables of your inputs. They are just placeholders which can be used inside your function only. You can also use another parameters For example: Y = hypotenuse(4, 2) Z = hypotenuse(5, 9) On Mon, Jul 22, 2024, 7:45 AM ThreeBlindQuarks via Tutor wrote: > This simple request has become too long. > > You can write a program like this fairly simply in any language without > needing to start with a LISP Dialect and trying to sort of translate it > into Python. > > The code offered has two parts and an amazing number of errors. > > But start over and think in Python. > > How does one define a function and specify it takes two variables? > > How does one check if the two variables are of an appropriate kind and if > not, deal with it either by coercing them to the type you need or dealing > with or propagating an error? > > Assuming the two arguments are reasonable, as in integers or floating > point or some class you can do arithmetic with, how does one compute > squares of numbers, add them together and take a positive square root? > > How does one return a value from a function, in this case probably a > single floating point number? > > How do you test a function with some suite of tests and validate the > results? > > On to your next part. It sounds like you want an interactive program that > asks the user for two numbers for the length of the base and height of some > right triangle and call the function to calculate the length of the > hypotenuse and presumably print it out. > > The very first thing you need is to write out a description, perhaps your > own way, but ideally consider things like what I am writing. > > You then need to research not just how to create a function, but what > Python functionality you can use for the rest. How does one print things > that a user can read? How does one read the answer? In what form is the > answer? Is it characters or some form of number or maybe something else? > What form does it need to be as an argument to the function? Realistically, > there can be many possibilities as you are also writing the function! > > For example, here is my slightly odd implementation, as explained below: > > # CODE START > > import math > > def hype(base, height): > return math.sqrt(float(base)**2 + float(height)**2) > > hype(input("Base: "), input("Height: ")) > > # CODE END > > What does this do? This being Python and not LISP, my function takes > anything that can validly be coerced to a floating point and then uses it > as changed to return a float result. Either or both inputs can be integers > or floating point numbers but also text as in "5.3" and with some effort, > it could probably be made to handle binary and octal and hexadecimal and > fractional representations. For now, it does not check for errors as that > would need thought and lots more code. > > All it does is hope converting to float works and apply the formula and > return the result. > > But since it works on strings containing proper numbers, you can call it > with a pair of input statements that can be seen in this transcript with > examples and the text returned by input is converted: > > >>> hype(input("Base: "), input("Height: ")) > Base: 3 > Height: 4.0 > 5.0 > >>> hype(input("Base: "), input("Height: ")) > Base: 5.0e2 > Height: 12.1e3 > 12110.326172320876 > > No, this is not what you should design but is an example of showing your > design can be done often in many ways as long as the parts are built to > work together. > > If you were sure the arguments would be exactly say integers, fine. If not > sure, your code might repeatedly try to evaluate what kind of argument you > got and try coercing it various ways to get a result you want. As an > example, float() does not convert a string containing binary or hexadecimal > or float. But doing something like this as an experiment: > > >>> int("0b111", 2) > 7 > > >>> int("0b111", 8) > Traceback (most recent call last): > File "", line 1, in > ValueError: invalid literal for int() with base 8: '0b111' > > The above could be used in a try clause to try various conversions and see > what sticks and enlarge possible uses of the function a lot but for your > purposes, is it worth the bother? My guess is your current needs are simple > but you should handle what happens if the user enters no numbers or > something invalid. > > So make a clear design and implement THAT. > > > > > > > > > > Sent with Proton Mail secure email. > > On Saturday, July 20th, 2024 at 12:20 PM, 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 = leg12 + leg22 > > 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 > > > > > > > > > > I have the same trouble when I run the program as a script in the > > terminal but when I use interactive mode I have no difficulties. > > > > > > The same program in Scheme is > > > > (define (hypotenuse leg1 leg2) (sqrt (+ (expt leg1 2) (expt leg2 2)))) > > (hypotenuse 3 4) > > > > and works wonders. Why doesn't my Python program run? What do I do > > wrong? Can someone help me out? Perhaps give me some pointers as to what > > I should learn? > > > > Thanks in advance and best regards, > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From torbjorn_svensson_diaz at yahoo.com Mon Jul 22 07:21:35 2024 From: torbjorn_svensson_diaz at yahoo.com (Torbjorn Svensson Diaz) Date: Mon, 22 Jul 2024 13:21:35 +0200 Subject: [Tutor] Print result of function doesn't work In-Reply-To: References: Message-ID: On 7/20/24 6:20 PM, Torbjorn Svensson Diaz via Tutor wrote: > Hello, dear tutors! > Hello, dear tutors! I've made it! Wohoo! The following works. import math print("What is the length of the first leg of the triangle?") leg1 = float(input()) print("What is the length of the second leg of the triangle?") leg2 = float(input()) def hypotenuse(leg1, leg2): ??? legs = leg1**2 + leg2**2 ??? result = math.sqrt(legs) ??? return result x = hypotenuse(leg1, leg2) print(x) It turns out I had to convert the input into a float as it's a string by default. Thanks for all your advice and guidance! From PythonList at DancesWithMice.info Mon Jul 22 18:28:22 2024 From: PythonList at DancesWithMice.info (dn) Date: Tue, 23 Jul 2024 10:28:22 +1200 Subject: [Tutor] Print result of function doesn't work In-Reply-To: References: Message-ID: <42c245fc-ac76-4376-b040-dd2af3059f3f@DancesWithMice.info> On 22/07/24 23:21, Torbjorn Svensson Diaz via Tutor wrote: > > On 7/20/24 6:20 PM, Torbjorn Svensson Diaz via Tutor wrote: >> Hello, dear tutors! >> > > Hello, dear tutors! > > I've made it! Wohoo! Good! You mentioned using Thonny. Did you use View > Variables and Run > Debug? -- Regards, =dn From torbjorn_svensson_diaz at yahoo.com Tue Jul 23 05:53:49 2024 From: torbjorn_svensson_diaz at yahoo.com (Torbjorn Svensson Diaz) Date: Tue, 23 Jul 2024 11:53:49 +0200 Subject: [Tutor] Print result of function doesn't work In-Reply-To: <42c245fc-ac76-4376-b040-dd2af3059f3f@DancesWithMice.info> References: <42c245fc-ac76-4376-b040-dd2af3059f3f@DancesWithMice.info> Message-ID: <286f2afd-afeb-4d34-8635-fc0aa2a70a53@yahoo.com> On 7/23/24 12:28 AM, dn via Tutor wrote: > On 22/07/24 23:21, Torbjorn Svensson Diaz via Tutor wrote: >> >> On 7/20/24 6:20 PM, Torbjorn Svensson Diaz via Tutor wrote: >>> Hello, dear tutors! >>> >> >> Hello, dear tutors! >> >> I've made it! Wohoo! > > Good! > > You mentioned using Thonny. Did you use View > Variables and Run > Debug? No, but maybe I should have, From PythonList at DancesWithMice.info Tue Jul 23 15:37:36 2024 From: PythonList at DancesWithMice.info (dn) Date: Wed, 24 Jul 2024 07:37:36 +1200 Subject: [Tutor] Print result of function doesn't work In-Reply-To: <286f2afd-afeb-4d34-8635-fc0aa2a70a53@yahoo.com> References: <42c245fc-ac76-4376-b040-dd2af3059f3f@DancesWithMice.info> <286f2afd-afeb-4d34-8635-fc0aa2a70a53@yahoo.com> Message-ID: On 23/07/24 21:53, Torbjorn Svensson Diaz via Tutor wrote: > > On 7/23/24 12:28 AM, dn via Tutor wrote: >> On 22/07/24 23:21, Torbjorn Svensson Diaz via Tutor wrote: >>> >>> On 7/20/24 6:20 PM, Torbjorn Svensson Diaz via Tutor wrote: >>>> Hello, dear tutors! >>>> >>> >>> Hello, dear tutors! >>> >>> I've made it! Wohoo! >> >> Good! >> >> You mentioned using Thonny. Did you use View > Variables and Run > Debug? > > No, but maybe I should have, Recommend spending some time looking at the tools being used. In this case, very useful for understanding how Python/the computer works, and thus, debugging. However, it is a subject of debate amongst tutors and trainers (and book authors), because learning Python requires cognitive effort and learning about tools even more on top of that. Concerns about over-load! However, of equal weight concerns when proving one's Python-learning by coding leads to frustration, eg "What do I do wrong?". Regardless, well done! -- Regards, =dn