I am trying to figure out how to use a for loop to identify a quadrant that a point (defined by a tuple) is in.
The program prompts until a non-numerical answer is given. Then, using the points input by the user, I need to identify which quadrant each point would be in.
For example: points (0, 1),(1,2),(2,3), and (3,4) are stored in a list and need their quadrants identified by a for loop.
I am unsure where to start with this. My input code is as follows:
locationlist = []
count = 0
while True :
try :
tup = input("Enter X and Y separated by a space, or enter a non-number to stop: ")
x, y = tup.split()
x = float(x)
y = float(y)
count = count + 1
locationlist.append(tup)
except ValueError:
break
print("Points: ",locationlist)
I believe this is what you are asking for:
for x, y in locationList:
a = x > 0
b = y > 0
c = x == 0 or y == 0
if not c:
if a and b:
# 1st quadrant
elif a:
# 4th quadrant
elif b:
# 2nd quadrant
else:
# 3rd quadrant
else:
# no quadrant
Basically what I'm doing is storing the conditions of whether x and y are positive or not, so that I can do logic on those without repeatedly calculating those conditions.
You don't need to create extra variables for that, just nest your test.
for x, y in locationList:
if y >= 0:
if x >= 0: print( x, y, 'Quadrant 1' )
else: print( x, y, 'Quadrant 2' )
else: ## y < 0
if x < 0: print( x, y, 'Quadrant 3' )
else: print( x, y, 'Quadrant 4' )
Edit: If axis is required:
for x, y in locationList:
if y > 0:
if x > 0: print( x, y, 'Quadrant 1' )
elif x < 0: print( x, y, 'Quadrant 2' )
else: print( x, y, 'pos Y-axis' )
elif y < 0
if x < 0: print( x, y, 'Quadrant 3' )
elif x > 0: print( x, y, 'Quadrant 4' )
else: print( x, y, 'neg Y-axis' )
else:
if x > 0: print( x, y, 'pos X-axis' )
elif x < 0: print( x, y, 'neg X-axis' )
else: print( x, y, 'Origin' )
I think I would use a divide-and-conquer strategy to reduce the number of comparisons. Perhaps something like this:
for x, y in locationList:
# 2nd or 4th
if x * y < 0:
if x < 0:
# Second quadrant.
else:
# Fourth quadrant.
# 1st or 3rd
elif x * y > 0:
if x < 0:
# Third quadrant.
else:
# First quadrant.
# On an axis: quadrant not well-defined.
else:
# No quadrant.
Related
I want to change location by putting command but got trouble in for loop
All I want is put R R R U D D and get (3,4) location
here is my code
x,y = first start point
N= size of map
N=5
x,y = 1,1
I define left(-1,0), right(1,0), upper(0,-1), down(0,1)
def L(x,y):
if x>1 and x<N and y>1 and y<N:
x=x
y=y-1
return(x,y)
else:
return(x,y)
def R(x,y):
if x<N and y<N:
x=x
y=y+1
return(x,y)
else:
return(x,y)
def U(x,y):
if x>1 and x<N and y>1 and y<N:
x=x-1
y=y
return(x,y)
else:
return(x,y)
def D(x,y):
if x<N and y<N:
x=x+1
y=y
return(x,y)
else:
return(x,y)
input command
move_type=L(x,y),R(x,y),U(x,y),D(x,y)
num = [*map(int, input().split())]
put num [1 1]
change location - this is the point where I got trouble
for i in num:
x,y = move_type[i]
print(x,y)
**result come like this
1 2
1 2
I expect (1,2)
(1,3)
what's wrong with my code
help me plz**
Run Method like that
num = [*map(int, input().split())]
x, y = num
# move_type=L(x,y),R(x,y),U(x,y),D(x,y)
# above tuple has the return value of function because you call them with the x and y as 1.
move_type = L, R, U, D # But here I only provide the function name to
# tuple and later I execute them with the new x and y values
for i in num:
x, y = move_type[i](x, y) # calling the function with the new value of x and y
print(x, y)
One suggestion change your all functions
def L(x, y):
if x > 1 and x < N and y > 1 and y < N: # if this if execute then the x and y modify and return at the end
x = x
y = y-1
# but if if not execute the x and y values return to the same x and y values
return (x, y) # this will return the x, y
def R(x, y):
if x < N and y < N:
x = x
y = y+1
return (x, y)
def U(x, y):
if x > 1 and x < N and y > 1 and y < N:
x = x-1
y = y
return (x, y)
def D(x, y):
if x < N and y < N:
x = x+1
y = y
return (x, y)
I am having difficulties getting this code to work. I am attempting to do the following.
When x is raised to a power of y, it needs to be true except with number 1.
Return true when x is divisible by y.
Problems.
I wrote this code but the problem is that when I input x=3 and y=3 I get and answer that x is the power of y which isn't true. Regardless of the number, if x == y it always says that x is power of y.
Question.
How would you make the corrections for this code?
def is_divisible(x, y):
if x % y == 0:
print("x is divisible by y")
return True
else:
print("x is not divisible by y")
print("x is not the power of y")
return False
def is_power(x, y):
if x == 1 or y == 1: # code to test the base of 1
return x == 1
elif x % y == 0:
print("x is the power of y")
print("x is divisible by y")
elif is_divisible(x, y) and is_power(x / y, y): # recursion of is_divisible. This will be true only if x/y
# then x is the power of y.
return 0.0
else:
return False
print(is_power(2, 2))
outputs
x is the power of y
x is divisible by y
None
print(is_power(27, 3))
x is the power of y
x is divisible by y
None
def choose (x, y):
if y > x:
print ("False")
elif y == 0 or y == x:
return 1
elif y == 1:
return x
else:
if (x-y) > y:
biggest = x-y
smallest = y
else:
biggest = y
smallest = x-y
resultatet = x * choose (x-1, biggest)
res = resultatet // smallest
return res
My function is working perfectly with whatever x input I insert but with bigger Y inputs like 8000 for example I'm getting
File "/home/nazel607/labb3b_2.py", line 20, in choose
resultatet = x * choose (x-1, biggest)
File "/home/nazel607/labb3b_2.py", line 3, in choose
if y > x:
RuntimeError: maximum recursion depth exceeded in comparison
Is there a way I can overcome this problem or is it impossible in python due to its limits? Is there another way than increasing the limit?
It seems that you can get rid of the recursion:
def choose2(x, y):
if y > x:
raise ValueError()
if y == 0 or y == x:
return 1
if y == 1:
return x
result = 1
while y != x:
big, small = max(x-y, y), min(x-y, y)
result *= x // small
x -= 1
y = big
return result
I've tested it over few examples:
for x, y in [
(4, 2),
(17, 9),
(125, 79),
(8005, 13),
(9005, 13),
# (19005, 7004) # exceeds max recursion depth on my machine
]:
assert choose(x, y) == choose2(x, y)
and seems to work fine.
You are not exiting the program ...
def choose (x, y):
if y > x:
print ("False")
return
# ...rest of your program
def max3bad(x,y,z):
maximum = 0
if x >= y:
if x >= z:
maximum = x
elif y >= z:
maximum = y
else:
maximum = z
return(maximum)
wrong output for what input?
get an input for which u get wrong output
for case: x = 2, y = 1, z = 3
the code output is 0 not 3.
It's better to get max number by :
max(x, y, z)
or you fix bug in your code:
def max3bad(x,y,z):
maximum = 0
if x >= y:
if x >= z:
maximum = x
else:
maximum = z
elif y >= z:
maximum = y
else:
maximum = z
return maximum
This code is restarting the Python shell, and I cannot work out the errors of my code.
def middle(x,y,z):
if x > y and x < y:
return x
elif y > x and y < z:
return y
elif z > x and z < y:
return z
else:
return False
#Main Routine
middle(1,11,111)
Note that Python can chain comparisons for you (see the docs), and you are missing several cases:
def middle(x, y, z):
"""Return the middle of the three input values."""
if y < x < z or z < x < y: # or min(y, z) < x < max(y, z)
return x
elif x < y < z or z < y < z:
return y
elif x < z < y or y < z < x:
return z
return False
In use:
>>> middle(1, 11, 111)
11
If you want to see results when running the script directly, you will have to be explicit about this; as Martijn suggested in the comments, you could print middle(1, 11, 111). Otherwise the result will be evaluated, but not actually shown on-screen.
You can also simplify by sorting the inputs:
def middle(x, y, z):
"""Return the middle of the three input values."""
x, y, z = sorted((x, y, z))
return y if x < y < z else False
I am not sure what you want to do with this but your function looks weird to me
For example:
if x > y and x < y:
Will never be true
Also, if this function aims at returning the median of the three value it does not do this.
it would be more like
def middle (x, y, z):
t = [x, y, z]
t.sort()
return t[1]
Hope this helped