0

explain please

There is such a task We need to calculate the area of the rectangle. Complete and call the function to output the area of the rectangle, taking 2 arguments as length and width. If sides are equal, the function should also show "Square" as the second output. my code: length = int(input()) width = int(input()) def area(length, width): #your code if length*width: print (length*width) elif length==width: print(Square) area(length, width) why doesn't it output (square)? displays everything except (square)

6th Aug 2021, 6:07 AM
Никита Белоусов
Никита Белоусов - avatar
5 Respuestas
+ 3
Hi! In python, if statement evaluates true anything except empty string and 0 value. That's why output is always length *width for any integers when length!=0 and width!=0. So, if statement should be like this if length != width: Second thing is that strings must be quoted with single or double quotes to declare as a variable. print("Square")
6th Aug 2021, 6:25 AM
Python Learner
Python Learner - avatar
+ 1
Square should be in quotes likes "Square", also the if statement will work no matter what, so the elif statement won't work and won't print "Square"
6th Aug 2021, 6:15 AM
Eashan Morajkar
Eashan Morajkar - avatar
+ 1
Please tag the question properly https://code.sololearn.com/W3uiji9X28C1/?ref=app
6th Aug 2021, 8:16 AM
Ipang
0
length = int(input()) width = int(input()) def area(length, width): #your code if length!=width: print (length*width) else: print(f"{length*width}\nSquare") area(length, width)
6th Aug 2021, 6:29 AM
Eashan Morajkar
Eashan Morajkar - avatar
0
length = int(input()) width = int(input()) def area(length, width): answer = length * width if length != width: print (f"Rectangle: {answer}") elif length == width: print(f"Square: {answer}") area(length, width)
6th Aug 2021, 6:45 AM
Yamily Fernandez
Yamily Fernandez - avatar