1

I am new to programming and I have difficulties with solving a problem. I am coding it in python. May I have some help, please? So the condition says: A rectangular is set on 2 of its opposite angles (x1, x2) and (y1, y2). Find the area and the perimeter of the rectangular. The input is read from the console. The numbers x1, x2, y1, y2 are given one by one on a line.

Inputs and Outputs:

enter image description here

An example:

enter image description here

my code:

x1 = float(raw_input("x1 = "))
y1 = float(raw_input("y1 = "))
x2 = float(raw_input("x2 = "))
y2 = float(raw_input("y2 = "))

if x1 > x2 and y1 < y2:
    a = x1 - x2
    b = y2 - y1
else:
    a = x2 - x1
    b = y1 - y1

area = a * b
perimeter = 2 * (a + b)

print area
print perimeter
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 2
    You have a typo: `b = y1 - y1` (which is `0`). That should be `y2 - y1`? – roganjosh May 03 '17 at 18:45
  • So you want it for a rectangle or quadrilateral, because the user can enter anything, include coordinates for a irregular quadrilateral – ArmaGeddON May 03 '17 at 18:46
  • Welcome to [SO]! If you haven't yet, please visit [ask] and [mcve] for guidelines on the sort of questions that work well here. Your post seems to be missing a specific question. Precisely what are you asking? – Robᵩ May 03 '17 at 18:54
  • `a` and `b` just need to be the absolute difference of the two values; no `if` needed. `a = abs(x1 - x2)` and `b = abs(y1 - y2)`. (With `if`, you need to check `x` and `y` separately anyway.) – chepner May 03 '17 at 19:09
  • @ArmaGeddON Only two points define an infinite number of quadrilaterals, but they are sufficient to define a unique rectangle whose horizontal sides are parallel to the *x* axis. – chepner May 03 '17 at 19:12
  • It worked with the `abs()` method. Thank you for the help. – Daniel Yordanov May 03 '17 at 19:18

1 Answers1

1

You are on the right track!

Let me make a few suggestions:


Inputs

There is nothing wrong with this. (Unless you want to use Python3, where where raw_input is now just input)

x1 = float(raw_input("x1 = "))
y1 = float(raw_input("y1 = "))
x2 = float(raw_input("x2 = "))
y2 = float(raw_input("y2 = "))

Width and Height of the rectangle

If you use the builtin function abs(), you do not have to worry about the sign of (x1 - x2) and (y1 - y2)! Where abs(x) gives the absolute value of x.

width = abs(x1 - x2)
height = abs(y1 - y2)

Area and perimeter

Now that we have the height and width, we can use you code to calculate the area and perimeter:

area = height * width
perimeter = 2 * (height + width)

Bonus

Check if your rectangle is a square:

if height == width:
    print "It's a square!"

Putting everything together:

x1 = float(raw_input("x1 = "))
y1 = float(raw_input("y1 = "))
x2 = float(raw_input("x2 = "))
y2 = float(raw_input("y2 = "))

width = abs(x1 - x2)
height = abs(y1 - y2)

area = height * width
perimeter = 2 * (height + width)

print area
print perimeter

if height == width:
    print "It's a square!"

Let me know if you need me to explain anything!

tpvasconcelos
  • 671
  • 7
  • 19
  • `x1 = float(input("x1 = ")) y1 = float(input("y1 = ")) x2 = float(input("x2 = ")) y2 = float(input("y2 = ")) a = abs(x1 - x2) b = abs(y1 - y2) area = a * b perimeter = 2 * (a + b) print area print perimeter` I tried using only `input()` and it still works but I'd like to know if there is a difference in 'raw_input()' and 'input()' or the difference is only that in Python 2 is `raw_input` and in Python 3 is `input()`. – Daniel Yordanov May 04 '17 at 08:15
  • In Python 2 there **is** a difference! In Python 3 there only exist `input`, which is the same as the old `raw_input` and the old `input` does not exist anymore. See (the difference between raw_input and input)[http://stackoverflow.com/questions/4915361/whats-the-difference-between-raw-input-and-input-in-python3-x] for more detail. – tpvasconcelos May 04 '17 at 09:59