#๐ How do I use the return function on this?
44 messages ยท Page 1 of 1 (latest)
@flint root
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.
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()
what return function ? do you mean how to return from the function
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
what exactly do you want the code to do ?
i'm guessing you want to exit when input is 4
yes
It is
then what is the problem
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
ah okay should be easy enough instead of printing you just return the calculations
then if need be print after the function call
what do you mean?
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
ahh, I kinda get it now, but how does the return fn work exactly?
like why use it instead of print
it basically does what it sounds like it ends the function and goes back to its caller with whatever value you return
There is NO return in the code you originally posted.
I meant how do I add return fn in this code mb, that was my sentence mistaking
I see
check the gif posted by bot its nice
Get rid of all that printing in your functions and just return the result. Do the printing (AND the input) somewhere else.
Yeah I did that jut now
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?
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
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.