Python Forum
python 3.4.3 Question a defining function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python 3.4.3 Question a defining function
#1
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)
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Defining x and y axes in Spectrogram Function amy5678 3 2,280 Nov-29-2020, 01:42 PM
Last Post: jefsummers
  Question on "define function"; difference between return and print extricate 10 4,729 Jun-09-2020, 08:56 PM
Last Post: jefsummers
  Function question JimBob9432 2 2,443 Oct-02-2019, 02:49 AM
Last Post: JimBob9432
  defining a function to see if a list is sorted Siylo 14 7,003 Nov-29-2018, 05:25 PM
Last Post: wavic
  Python Tutorial range function question HawkeyeKnight 2 2,707 Sep-27-2018, 09:45 AM
Last Post: ThiefOfTime

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020