-1

Hello dear SO members and staffs. I have been working on a project where I took the coordinates of the already drawn rectangle with the center of (0,0) coordinates. What I want to do is ask the user to put his x and y coordinates, after which it will tell you that whether if your coordinates is within the area of that or not. I have reached some of my goals, except the one that I need to ask the if statement for both of the x and y at the same time as if I will write only X statement it will display for only checking the X and not Y. So, I need your help in how to check both of them before displaying? (The center of the rectangle is at the (0,0) with the total length of 5 and the width of 10.)

y1 = -2.5
y2 = 2.5
x1 = -5
x2 = 5


inputX = eval(input("Please put in the X coordinate: "))
inputY = eval(input("Please put in the Y coordinate: "))

if x1<inputX<x2, y1<inputY<y2:
    print("Your coordinates are in the range of the rectangle!")
else:
    print("Sorry, your coordinates are not in the range of the rectangle!")
Vusal Ismayilov
  • 111
  • 2
  • 9
  • Your current code checks if a non-empty tuple is true (which it will always be). – chepner Oct 17 '19 at 17:08
  • 1
    Why would you use `eval` on the user input? If you're expecting an integer, use `int`, or `float` if you want a float, but don't, don't use `eval`! – Thierry Lathuille Oct 17 '19 at 17:09
  • @ThierryLathuille the thing is the user can also type some float numbers also, so eval basically makes it also possible to calculate that is why I used it. – Vusal Ismayilov Oct 17 '19 at 17:12
  • Putting `eval` around the input exposes your program to the user running effectively any arbitrary Python code they want. See [here](https://stackoverflow.com/a/9383764/4739755) for some more explanation, and a reason in the comments for why this can be _very bad_. – b_c Oct 17 '19 at 17:18
  • This is literally the **worst** place you can use `eval` – Alan Kavanagh Oct 18 '19 at 11:42

3 Answers3

1

Use and to combine them:

if (x1 < inputX < x2) and (y1 < inputY < y2):
Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
0

i think that will work

if (x1<inputX and inputX<x2 and y1<inputY and inpputY<y2):
    print("Your coordinates are in the range of the rectangle!")
else:
    print("Sorry, your coordinates are not in the range of the rectangle!")
bob_1982
  • 715
  • 6
  • 16
0
The following code will work for only integer inputs - as it uses range function - range()

`

x1 = -5
x2 = 5
y1 = -2
y2 = 2



inputX = eval(input("Please put in the X coordinate: "))
inputY = eval(input("Please put in the Y coordinate: "))

if (inputX in range(x1,x2+1)) and (inputY in range(y1,y2+1)):
    print("Your coordinates are in the range of the rectangle!")
else:
    print("Sorry, your coordinates are not in the range of the rectangle!")

`

The answer before given using if condition has no problem, I am writing this to give an idea for an approach of using it using a function
kush_shah
  • 137
  • 2
  • 9