Home > Software design >  problems with unpacking returned values in tuples
problems with unpacking returned values in tuples

Time:10-19

I have a function that gets arguments of the file, this function looks for the coordinates of this file on the screen and then returns them. I know that this question could be very stupid but please help.

redx,redy = somefunc(Ui_Ellements[9], 12)

def somefunc(file,index):
    .....
    x=int((pt[0]*2 w)/2)
    y=int((pt[1]*2 h)/2)
    print("Found " file,x,y)
    if index==12:
        return (x,y)

All should be working correctly, but I have such a problem

redx,redy = somefunc(Ui_Ellements[9], 12) TypeError: cannot unpack non-iterable int object

I tried:
1.

def move(index):
    print(index)
    for i in index:
        print(i)
red = somefunc(Ui_Ellements[9], 12)
move(red)

def somefunc(file,index):
    .....
    x=int((pt[0]*2 w)/2)
    y=int((pt[1]*2 h)/2)
    print("Found " file,x,y)
    if index==12:
        return (x,y)
red = somefunc(Ui_Ellements[9], 12)
print(red[0])
red = somefunc(Ui_Ellements[9], 12)
print(red[-1])

P.s all data is correct and if I try

print(red)

it outputs data

(1205, 306)


def somefunc(file,index):
global sens
global top
global left
global Resolution
time.sleep(speed)
if index ==7 or index ==12 and file != chekers[8] :
    img = cv2.imread('files/' Resolution '/part.png')
else:
    screen()
    img = cv2.imread('files/' Resolution '/screen.png')  # картинка, на которой ищем объект
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # преобразуем её в серуюш
template = cv2.imread('files/' Resolution '/' file,cv2.IMREAD_GRAYSCALE)  # объект, который преобразуем в серый, и ищем его на gray_img
w, h = template.shape[::-1]  # инвертируем из (y,x) в (x,y)`
result = cv2.matchTemplate(gray_img, template, cv2.TM_CCOEFF_NORMED)
loc = np.where(result >= sens)
if index =='battletag':
    print(loc)
    if len(loc[0]) != 0:
        j=0
        for pt in zip(*loc[::-1]):
            Flag=False
            for num in range(3):
                if pt[0] < enemywiz[num]   10 and pt[0] > enemywiz[num] - 10:
                    Flag=True
                    pass
            if not Flag:
                for num in range(3):
                    if enemywiz[num] == 0 :
                        enemywiz[num]=pt[0]
                        x = (pt[0] * 2   w) / 2
                        y = (pt[1] * 2   h) / 2
                        enemywizF[j]=x,y
                        j =1
                        break
        for i in range(2):
            enemywiz[i]=0
        return 0
if len(loc[0]) !=0:
    for pt in zip(*loc[::-1]):
        pt[0]   w
        pt[1]   h
    x=int((pt[0]*2 w)/2)
    y=int((pt[1]*2 h)/2)
    print("Found " file,x,y)
    if index==12:
        return (x,y)
    if (index==6 or file == Ui_Ellements[5] or file == chekers[7]):
        global xm
        global ym
        xm=x
        ym=y
        return True
    if file == chekers[8]:
        if index ==7:
            xm =left
            ym =top
        ahk.mouse_move(xm, ym, speed=2)
        time.sleep(0.5)
        ahk.mouse_drag(x, y, relative=False)
        return True
    if file == chekers[5]:
        ahk.mouse_move(x, y 70, speed=3)  # Moves the mouse instantly to absolute screen position
        ahk.click()
        return True
    if file ==buttons[5]:
        ahk.mouse_move(x, y, speed=5)
        return True
    if index == 1:
        return True
    if index == 7:
        xm = x
        ym = y
        return True
    ahk.mouse_move(x, y, speed=5)  # Moves the mouse instantly to absolute screen position
    ahk.click()  # Click the primary mouse button
    if file ==buttons[7]:
        return True
    if file == Ui_Ellements[3]:
        group_create()
else:
    print("Not found  " file)
    if index == 12:
        return 0

CodePudding user response:

  • You need keep a function has basically same type of output in most of common situation.
  1. you can replace return 0 to return 0,0
  2. or you can replace it to raise Exception(f"Not found {file}")
  • I don't know what you exactly want to return when you return True, you can also replace it with same style, such as raise Exception(f"can't get redx and redy").

example:

def test(x,y):
    index = x   y
    if index == 12:
        return (x,y)
    else:
        raise Exception("Not found file")
        # return 0,0
try:
    a,b = test(2,3)
    print(a,b)
except Exception as e:
    print(e)

try:
    a,b = test(7,5)
    print(a,b)
except Exception as e:
    print(e)

result:

Not found file
7 5
  • Related