Getting x from given (x, y) data?
Question:
I’m making a program that makes vertical and horizontal lines. When a user clicks somewhere on the GUI it gives me a (x,y) value, but I just need one depending on if they wanted a horizontal line or vertical line. How would I extract those values? For the record I’m using pygame zero(Yes I know it is very basic), I just receive a (x,y) tuple.
Answers:
I’m assuming your (x,y)
value is a tuple. So, here is how you extract data from a tuple:
x = 1
y = 2
your_value = (x, y)
print(your_value[0]) # prints 1 (x value)
print(your_value[1]) # prints 2 (y value)
Alternatively:
x = 1
y = 2
my_tuple = (x, y)
value_x, value_y = my_tuple
print(value_x) # prints 1
print(value_y) # prints 2
I will try to give an answer, but next time please put more detail into your question.
I cannot say for sure, but I’m assuming your function for getting x,y returns a tuple.
If so you can do the following:
x, y = getXY()
print(x) // change to y if needed
Now, in your question you stated you asked the user if they wanted a horizontal or vertical line. You could implement that like the following:
x, y = getXY()
print(x) // change to y if needed
choice = ""
while choice != "h" or choice != "v":
choice = input("Line type (h for horizontal, v for vertical)")
if choice == "h":
print(x) // and draw a horizontal line
else
print(y) // and draw a vertical line
Remember, next time please put more information in your question.
If the code you are using is returning the data as a tuple, then to ge the values just do val1, val2 = (x,y)
I’m making a program that makes vertical and horizontal lines. When a user clicks somewhere on the GUI it gives me a (x,y) value, but I just need one depending on if they wanted a horizontal line or vertical line. How would I extract those values? For the record I’m using pygame zero(Yes I know it is very basic), I just receive a (x,y) tuple.
I’m assuming your (x,y)
value is a tuple. So, here is how you extract data from a tuple:
x = 1
y = 2
your_value = (x, y)
print(your_value[0]) # prints 1 (x value)
print(your_value[1]) # prints 2 (y value)
Alternatively:
x = 1
y = 2
my_tuple = (x, y)
value_x, value_y = my_tuple
print(value_x) # prints 1
print(value_y) # prints 2
I will try to give an answer, but next time please put more detail into your question.
I cannot say for sure, but I’m assuming your function for getting x,y returns a tuple.
If so you can do the following:
x, y = getXY()
print(x) // change to y if needed
Now, in your question you stated you asked the user if they wanted a horizontal or vertical line. You could implement that like the following:
x, y = getXY()
print(x) // change to y if needed
choice = ""
while choice != "h" or choice != "v":
choice = input("Line type (h for horizontal, v for vertical)")
if choice == "h":
print(x) // and draw a horizontal line
else
print(y) // and draw a vertical line
Remember, next time please put more information in your question.
If the code you are using is returning the data as a tuple, then to ge the values just do val1, val2 = (x,y)