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)
5 Respostas
+ 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")
+ 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"
+ 1
Please tag the question properly
https://code.sololearn.com/W3uiji9X28C1/?ref=app
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)
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)