How to loop through a code only once then loop through other code over and over?

Viewed 47
def urlCopy():
    py.moveTo(x=420,y=11,duration=2)
    py.click()
    #share
    py.moveTo(x=1054,y=946,duration=2)
    py.click()
    #copy
    py.moveTo(x=1175,y=630,duration=2)
    py.click()
    #1st tab
    py.moveTo(x=252,y=16,duration=2)
    py.click()
    py.moveTo(x=1093, y=517)
    py.click() 
    if scrol == True:
        py.moveTo(x=154, y=420,duration=2)
        py.tripleClick()
        py.moveTo(x=154, y=420,duration=2)
        py.tripleClick()
        varrs = py.hotkey('ctrl','v')
        global scrol
        scrol = False
        
    else:
        py.scroll(-300)
        py.tripleClick()
        py.moveTo(x=154, y=420,duration=2)
        py.tripleClick()
        varrs = py.hotkey('ctrl','v')

I am trying to copy youtube videos title and save it in other website.I want the code to run without scrolling at first loop and then always scrolling with next loops.I am going to loop this function again and again.

1 Answers

You could use this:

scrol = True
def urlCopy():
    global scrol #define scrol as global before you reference it
    py.moveTo(x=420,y=11,duration=2)
    py.click()
    #share
    py.moveTo(x=1054,y=946,duration=2)
    py.click()
    #copy
    py.moveTo(x=1175,y=630,duration=2)
    py.click()
    #1st tab
    py.moveTo(x=252,y=16,duration=2)
    py.click()
    py.moveTo(x=1093, y=517)
    py.click() 
    if scrol == True: #referenced here
        py.moveTo(x=154, y=420,duration=2)
        py.tripleClick()
        py.moveTo(x=154, y=420,duration=2)
        py.tripleClick()
        varrs = py.hotkey('ctrl','v')
        scrol = False
        
    else:
        py.scroll(-300)
        py.tripleClick()
        py.moveTo(x=154, y=420,duration=2)
        py.tripleClick()
        varrs = py.hotkey('ctrl','v')
Related