[Tutor] Print result of function doesn't work
rauf ahmad
ahmadraufbd at gmail.com
Mon Jul 22 00:11:37 EDT 2024
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 <tutor at python.org>
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 "<string>", line 1, in <module>
> 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
> <tutor at python.org> 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
> > <function hypotenuse at 0x7fa05d5a1940>
> >
> >
> >
> > 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
>
More information about the Tutor
mailing list