0

What's wrong with my def code?

I need to calculate the area of a rectangle. What's wrong with my code? length = int(input()) width = int(input()) def area (length, width): print(length*width) if length == width: print('Square')

29th Dec 2021, 8:35 PM
ariPT
13 Antworten
+ 4
What is it supposed to do exactly? You have never called your function...
29th Dec 2021, 8:38 PM
Lisa
Lisa - avatar
+ 4
You can do it like this: 1. Define a function 2. Get the the input 3. Call the function def area(a, b): if a == b: print("Square") print(a*b) length = int(input()) width = int(input()) area(length, width)
29th Dec 2021, 8:56 PM
Coding Cat
Coding Cat - avatar
+ 2
def area(a, b): print(a * b) if a == b: print("Square")
29th Dec 2021, 10:02 PM
Coding Cat
Coding Cat - avatar
+ 1
Call the function before printing 'Square'
30th Dec 2021, 2:03 AM
Emerson Prado
Emerson Prado - avatar
0
Supposed to multiply length by width. And print 'Square' if they are identical
29th Dec 2021, 8:43 PM
ariPT
0
How do I make a code that will show the result first, and then Square? I tried moving it around, and it showed errors
29th Dec 2021, 9:52 PM
ariPT
0
ariPT call area function before "if length == width:"
31st Dec 2021, 11:52 AM
Lorenzo Pellegrini
Lorenzo Pellegrini - avatar
0
Or simply remove function declaration and leave print instruction
31st Dec 2021, 11:52 AM
Lorenzo Pellegrini
Lorenzo Pellegrini - avatar
0
Thank you all. I'm very much a beginner, and am still having trouble, despite your suggestions. Could one of you please write a full code? I'm trying to see what I missed
31st Dec 2021, 12:13 PM
ariPT
0
def area(a, b): print(a*b) if a == b: print("Square") length = int(input()) width = int(input()) area(length, width)
31st Dec 2021, 1:13 PM
Coding Cat
Coding Cat - avatar
0
Thank you! Why do I need to define area both as a,b and as length,width?
31st Dec 2021, 1:43 PM
ariPT
0
You can call them what you want. This version should only show you, that the variables inside a function are others then the variables outside the function.
31st Dec 2021, 1:58 PM
Coding Cat
Coding Cat - avatar
0
Thank you very much, I appreciate your help
31st Dec 2021, 2:12 PM
ariPT