#๐Ÿ”’ How do I use the return function on this?

44 messages ยท Page 1 of 1 (latest)

flint root
#

Return

wicked grottoBOT
#

@flint root

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

flint root
#
def perimeter():
    base = int(input("Base: "))
    width = int(input("Width: "))
    perimeter = 2*(base+width)
    print("Given value: Base =", base, ", Width =", width)
    print("Formula: Perimeter = 2 * (Base + Width)")
    print("Final Answer:", perimeter)
    
def area():
    base = int(input("Base: "))
    height = int(input("Height: "))
    area = base*height
    print("Given value: Base =", base, ", Height =", height)
    print("Formula: Base * Height")
    print("Final Answer:", area)

def base():
    area = int(input("Area: "))
    height = int(input("Height: "))
    base = area/height
    print("Given value: Area =", area, ", Height =", height)
    print("Formula: Area / Height")
    print("Final Answer:", base)

while True:
    print("""Parallelogram Operations:
1. Perimeter
2. Area
3. Base
4. Exit""")
    x = int(input("Enter Operation Number: "))
    if x == 1:
        print(perimeter())
    elif x == 2:
        print(area())
    elif x == 3:
        print(base())
    elif x == 4:
        print("Exiting...")
        break
    else:
        print("Invalid input, please try again...")
    print()
round marsh
#

what return function ? do you mean how to return from the function

wicked grottoBOT
#
Print and return

Here's a handy animation demonstrating how print and return differ in behavior.

See also: /tag return

flint root
#

yes something like that

#

like it keeps saying "return" is outside function

round marsh
#

where are you using return ?

#

it should be inside a function

flint root
#

I do it in the if elif area, I think its outside

#

but I don't know how to make it be with def function

round marsh
#

what exactly do you want the code to do ?
i'm guessing you want to exit when input is 4

flint root
#

yes

round marsh
#

but there;s already break

#

is it not exiting ?

flint root
#

It is

round marsh
#

then what is the problem

flint root
#

After calculating the perimeter, area, base I will use the return function to get the values, but I'm using print fn

#

Cause the requirement says it is needed to use "return" function

round marsh
#

ah okay should be easy enough instead of printing you just return the calculations

#

then if need be print after the function call

flint root
#

what do you mean?

round marsh
#
def perimeter():
    base = int(input("Base: "))
    width = int(input("Width: "))
    perimeter = 2*(base+width)
    print("Given value: Base =", base, ", Width =", width)
    print("Formula: Perimeter = 2 * (Base + Width)")
    print("Final Answer:", perimeter)

instead of this we can do

def perimeter(base, width):
    perimeter = 2*(base+width)
    return perimeter

or

def perimeter():
    base = int(input("Base: "))
    width = int(input("Width: "))
    perimeter = 2*(base+width)
return perimeter
flint root
#

ahh, I kinda get it now, but how does the return fn work exactly?

#

like why use it instead of print

round marsh
#

it basically does what it sounds like it ends the function and goes back to its caller with whatever value you return

earnest phoenix
wicked grottoBOT
#
Print and return

Here's a handy animation demonstrating how print and return differ in behavior.

See also: /tag return

flint root
round marsh
#

check the gif posted by bot its nice

earnest phoenix
flint root
#

Thanks for the help ๐Ÿ™‚

#

Wait actually, I have another question

#

what's the difference between this and this?

#

Sample 1:

def area():
    base = int(input("Base: "))
    height = int(input("Height: "))
    area = base*height
    return str(area) + " Is the final answer"

Sample 2:

def bmi_calculator(name, height_m, weight_kg):
    bmi = weight_kg / (height_m **2)
    print("bmi:")
    print(bmi)
    if bmi < 25:
        return name + " is not overweight"
    else :
        return name + " is overweight"
#

Is the difference between having an argument and not having an argument?

round marsh
#

it depends what you want your func to do usually input should be handled outside the funcs you provide them with values they provide you with answers

#

either is fine but using arguments instead of asking for input inside functions is a better practice and should be followed, funcs should be only tasked with computing the answer not getting the input

flint root
#

I see

#

Thanks!

wicked grottoBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.