Pyperclip copy and dividing the results

224 Views Asked by At

I'm having some issues with my code not dividing the copied answers and instead giving me string errors.

for x in range(5):
    time.sleep(3)
    coordinates = (pyperclip.paste())
    print(coordinates/2) 

TypeError: unsupported operand type(s) for /: 'objc.pyobjc_unicode' and 'int'

2

There are 2 best solutions below

0
On BEST ANSWER

If you used pyperclip.copy("1234") and the string was a number then you could do this

for x in range(5):
    time.sleep(3)
    coordinates = int(pyperclip.paste())
    print(coordinates/2)
0
On

When you have a look on the main page of official site : https://pypi.org/project/pyperclip/

you have then the following code :

>>> import pyperclip
>>> pyperclip.copy('The text to be copied to the clipboard.')
>>> pyperclip.paste()
'The text to be copied to the clipboard.'

paste function does not essentially gives coordinates but you get back what has been copied.

If you copy a text, you can not divide a text by an integer. If you copy a number, let keep in mind that this number is a string until you defines it like a integer. For this you use the int() function. Let's say you copy a '2' :

import pyperclip
pyperclip.copy('2')
print(int(pyperclip.paste())/2)

Anyway in your case it won't work like you are trying to get coordinates. coordinates = (x , y) like it will generate the exception

c=(1,2)
c/2
Retraçage (dernier appel le plus récent) : 
  Shell Python, prompt 23, line 1
builtins.TypeError: unsupported operand type(s) for /: 'tuple' and 'int'

Let's say coordinates is the string '(1,2)', you could have then the new coordinates calculation function :

coordinates = '(1,2)'

def new_coord(coordinates, func):
    return tuple(map(func, coordinates.strip("()").split(",")))
    
print(new_coord(coordinates, func=lambda c: int(c)/2))
# (0.5, 1.0)