Problem when passing several lists in function with Python

Viewed 29

here is my script :

time_remaining = [1, 2, 3, 4]
url_links = ["a", "b", "c"]
nb_bookies = ['5', '7', '6']

def gitanerie(time, url, book):
    if len(time) != len(url):
        
        url = url.append('d')
        book = book.append('1')
        
    else:
        pass
    
    return url, nb_bookies

url_links, nb_bookies = gitanerie(time_remaining, url_links, nb_bookies)
print(url_links)
print(nb_bookies)

When I run it it gives me :

None
['5', '7', '6', '1']

the url_kinks is empty. How to solve this problem. Thanks

2 Answers

Don't use in place url and book bacause append has no return type value rather follow this,

def gitanerie(time, url, book):
    if len(time) != len(url):
        url.append('d')
        book.append('1')
    else:
        pass
    return url, nb_bookies

You are returning the wrong variables in your function and instead of book = book.append('d') use book.append('d')

time_remaining = [1, 2, 3, 4]
url_links = ["a", "b", "c"]
nb_bookies = ['5', '7', '6']

def gitanerie(time, url, book):
    if len(time) != len(url):
        url.append('d')
        book.append('1')
       
    else:
        pass
    return url, book
    
url_links, nb_bookies = gitanerie(time_remaining, url_links, nb_bookies)
print(nb_bookies,url_links)

This will result in:

['5', '7', '6', '1'] ['a', 'b', 'c', 'd']
Related