Chetan
Chetan

Reputation: 49

How to loop one by one when executing full script

below is my some part of python automation code: Inside def function for loop is there how to takes 1st value from list then continue with starting "kcauto()" then when it comes again in same loop then takes 2nd value from list and continue so on

my code : -

nnlist = [
'3789',
'4567'
]

def kcauto():
    ano = '031191'
    print(ano)
    code = '12'
    print(code)
    date = '06-Feb-2022'
    print(date)
    url2 = ('https://www.myweb&nn=')
    for nn in nnlist:
        callurl2 = print(url2 + nn)

for tn in nnlist:
    kcauto()
    print(tn)

My output : -

031191
12
06-Feb-2022
https://www.myweb&nn=3789
https://www.myweb&nn=4567
3789
031191
12
06-Feb-2022
https://www.myweb&nn=3789
https://www.myweb&nn=4567
4567

But required output : -

031191
12
06-Feb-2022
https://www.myweb&nn=3789
3789

031191
12
06-Feb-2022
https://www.myweb&nn=4567
4567

Upvotes: 1

Views: 48

Answers (1)

Nicolas Silva
Nicolas Silva

Reputation: 113

You have two loops going on in here. One outside of the kcauto() function and one inside of it. You must remove one of these in order to fix the double link print out.

Something like this might work:

nnlist = ['3789','4567']

def kcauto(items):
    for nn in items:
        ano = '031191'
        print(ano)
        code = '12'
        print(code)
        date = '06-Feb-2022'
        print(date)
        url2 = ('https://www.myweb&nn=')
        callurl2 = print(url2 + nn)
        print(nn + "\n")


kcauto(nnlist)

Upvotes: 1

Related Questions