## How to make lists equal a quadrilateral in Python?

Problem Description:

I am having difficulty figuring out to make user-entered lists of numbers equal a quadrilateral, specifically a rhombus and square, in Python. I do not know if my code needs a function or loops or if/else statements to know if it is a rhombus or square.

So far, I have this, but I can’t figure out how to go through each side to find out if they are equal to each other or if angle 1 equals angle 2, etc. Which afterwards, I would determine if it the numbers that the user entered made a list that equaled a rhombus or square. Excuse the confusing sentences, I tried to make my problem as clear as possible. Thank you for taking your time to help, if you do!

Edit: I tried a function, but found that it was too complicated by itself. I want to figure out how to search through each list’s elements and make sure that they equal/match each other. Thank you!

A rhombus has

- All four sides the same length
- Angle 1 equals Angle 3
- Angle 2 equals Angle 4

A square has

- All four sides the same length.
- All angles equal to each other

```
while True:
# Input, Validation, Repetition
print("=== Please enter Sides ===n")
sList = [] # list for sides
for i in range(0,4,1):
sides = float(input("Please enter side %i : " %(i + 1 ) ) )
if sides < 0:
sides = float(input("Value must be positive! Please enter side %i : " %(i + 1 )))
sList.append(sides)
print()
print("=== Please enter angles ===n")
aList = [] # list for angles
for i in range(0,4,1):
angles = float(input("Please enter angle %i : " %(i + 1 ) ) )
if angles < 0:
angles = float(input("Value must be positive! Please enter angle %i : " %(i + 1 )))
aList.append(angles)
print("=======================n")
# Rhombus
if sList == sList and aList[1] == aList[3] and aList[2] == aList[4]:
print("This is a rhombusn")
#Square
if sList == aList:
print("This is a square!n")
keep = input("Would you like to repeat? (1-Yes, 2-No): ")
if keep != '1':
break
```

## Solution – 1

**First we need some tweaking in the edge control.**

Because "if sides[i] == sides[i]:" statement returns always True.

Instead, let’s write a function that checks that all edges are the same.

```
def is_same_edges(edges) -> bool:
sum_edges = sum(edges)
return (sum_edges / len(edges)) == edges[0]
```

This function calculates the sum of all the elements in the list it takes as a parameter. Then it divides the length of the list it takes as a parameter and checks it with any of the lists element. If it is the same, all the elements are the same.

**Now that we’ve done the edge checks, we can move on to the angle check.**

```
def is_opposite_angles_same(angles) -> bool:
return angles[0] == angles[2] and angles[1] == angles[3]
```

This function returns whether the opposite angles are equal.

**Okay, we have the functions we need. Now let’s edit your function.**

```
def rhombus(sList, aList):
if is_same_edges(sList) and is_opposite_angles_same(aList):
rhombus = sList
print(rhombus)
```

## Solution – 2

Here’s an approach that should work.

First, define a function that evaluates your conditions to check whether something is a rhombus.

```
def is_rhombus(sList, aList):
if len(set(sList)) == 1 and aList[0] == aList[2] and aList[1] == aList[3]:
return True
else:
return False
```

`is_rhombus`

checks whether the set obtained by converting `sList`

into a set has a length of 1 – if yes, that means all members of `sList`

were equal to each other. It also checks whether opposite angles in `aList`

are equal to each other. if both conditions are satisfied, it returns `True`

, else returns `False`

.

Next, because we know all rhombuses are squares, but not necessarily the other way round, we create another function called `is_square`

:

```
def is_square(sList, aList):
if is_rhombus(sList, aList) and len(set(aList)) == 1:
return True
else:
return False
```

`is_square`

also takes `sList`

and `aList`

, and it calls the previously defined function `is_rhombus`

. If the shape IS a rhombus, `is_square`

checks whether in addition to being a `rhombus`

, all the angles are equal – using the same approach employed by `is_rhombus`

above.

Finally, with both these functions defined, we enter your `while True`

loop, and after we get the sides and angles from the user, we go into a little `if-elif-else`

block where we first check whether the shape is a square – because that’s the narrowest option available to us. If the shape is a square, we print a message to the screen accordingly. If it’s not a square, it could still be a rhombus, so we check for that. If it is a rhombus, we print that message to screen. If it’s not even a rhombus, we say it’s not a rhombus (no point also saying it’s not a square – if it’s not a rhombus, it can’t possibly be a square).

```
while True:
# Input, Validation, Repetition
print("=== Please enter Sides ===n")
sList = [] # list for sides
for i in range(0,4,1):
sides = float(input("Please enter side %i : " %(i + 1 ) ) )
if sides < 0:
sides = float(input("Value must be positive! Please enter side %i : " %(i + 1 )))
sList.append(sides)
print()
print("=== Please enter angles ===n")
aList = [] # list for angles
for i in range(0,4,1):
angles = float(input("Please enter angle %i : " %(i + 1 ) ) )
if angles < 0:
angles = float(input("Value must be positive! Please enter angle %i : " %(i + 1 )))
aList.append(angles)
print("=======================n")
# Rhombus
if is_square(sList, aList):
print ('This is a square!')
elif is_rhombus(sList, aList):
print ('This is a rhombus!')
else:
print ('This is not a rhombus.')
keep = input("Would you like to repeat? (1-Yes, 2-No): ")
if keep != '1':
break
```

Trialling this on a square:

```
=== Please enter Sides ===
Please enter side 1 : 40
Please enter side 2 : 40
Please enter side 3 : 40
Please enter side 4 : 40
=== Please enter angles ===
Please enter angle 1 : 90
Please enter angle 2 : 90
Please enter angle 3 : 90
Please enter angle 4 : 90
=======================
This is a square!
Would you like to repeat? (1-Yes, 2-No): n
```

Trialling this on a rhombus:

```
=== Please enter Sides ===
Please enter side 1 : 40
Please enter side 2 : 40
Please enter side 3 : 40
Please enter side 4 : 40
=== Please enter angles ===
Please enter angle 1 : 60
Please enter angle 2 : 120
Please enter angle 3 : 60
Please enter angle 4 : 120
=======================
This is a rhombus!
Would you like to repeat? (1-Yes, 2-No): n
```

Lastly, trialling it on a non-rhombus:

```
=== Please enter Sides ===
Please enter side 1 : 5
Please enter side 2 : 6
Please enter side 3 : 4
Please enter side 4 : 3
=== Please enter angles ===
Please enter angle 1 : 10
Please enter angle 2 : 20
Please enter angle 3 : 40
Please enter angle 4 : 30
=======================
This is not a rhombus.
Would you like to repeat? (1-Yes, 2-No): n
```

Of course it bears remembering that any shape with four equal sides is a rhombus. That opposite angles will be equal to each other is an attribute of rhombuses, not a criterion for something to be a rhombus. Recognising this, you could redefine your `is_rhombus`

function differently to ignore `aList`

altogether, and rely only on `sList`

to tell us whether the shape is a rhombus.

```
def is_rhombus(sList):
if sum(sList)/4 == sList[0]:
return True
else:
return False
```

Then, we change the way we call these functions – if the shape isn’t a rhombus, why make the user enter a bunch of angles? You could rearrange your `while True`

block like this:

```
while True:
# Input, Validation, Repetition
print("=== Please enter Sides ===n")
sList = [] # list for sides
for i in range(0,4,1):
sides = float(input("Please enter side %i : " %(i + 1 ) ) )
if sides < 0:
sides = float(input("Value must be positive! Please enter side %i : " %(i + 1 )))
sList.append(sides)
print()
if is_rhombus(sList):
print("=== Please enter angles ===n")
aList = [] # list for angles
for i in range(0,4,1):
angles = float(input("Please enter angle %i : " %(i + 1 ) ) )
if angles < 0:
angles = float(input("Value must be positive! Please enter angle %i : " %(i + 1 )))
aList.append(angles)
print("=======================n")
if is_square(sList, aList):
print ('This is a square!')
else:
print ('This is a rhombus!')
else:
print ('This is not a rhombus.')
keep = input("Would you like to repeat? (1-Yes, 2-No): ")
if keep != '1':
break
```

This behaviour is identical to before – except if four unequal sides are provided, we don’t bother asking for a bunch of angles.