python 3.4.3 Question a defining function - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: python 3.4.3 Question a defining function (/thread-5373.html) |
python 3.4.3 Question a defining function - student8 - Sep-30-2017 Write a function in_out(xs,ys,side) that takes three numbers as input, where side is non-negative. Here xs and ys represent the x and y coordinates of the bottom left corner of a square; and side represents the length of the side of the square. (Notice that xs, ys, and side completely define a square and its position in the plane). Your function should first prompt the user to enter two numbers that represent the x and y coordinates of some query point. Your function should print True if the given query point is inside of the given square, otherwise it should print False. A point on the boundary of a square is considered to be inside the square. (solve the questions without loops, if and other branching statements,lists) so far I have come up with this: def in_out(xs,ys,side): x= input ('Enter a number for the x coordinate of a query point:') y= input ('Enter a number for the y coordinate of a query point:') s1=int(x)<xs and int(x)<(xs+side) s2=int(y)<ys and int(y)<(ys+side) return s1 and s2 But I get the error message: Traceback (most recent call last): File "<pyshell#5>", line 1, in <module> in_out(0,0,2.5) File "C:\Users\User\Documents\ITI1120D\Assignments\assignment 1.py", line 35, in in_out s2=int(y)<ys and int(y)<(ys+side) ValueError: invalid literal for int() with base 10: '1.2' if this going to help another person I think I have figured out a solution def in_out(xs,ys,side): x= input ('Enter a number for the x coordinate of a query point:') y= input ('Enter a number for the y coordinate of a query point:') p1= float(x) >= xs and float(x)<= xs + side p2= float(y) >= ys and float(x)<= ys + side print (p1 and p2) RE: python 3.4.3 Question a defining function - nilamo - Oct-02-2017 It looks like you've already solved it... the error indicates that "1.2" is not a valid int, and using float() instead solves the issue.
|