How to divide code using functions Python 3.x

Viewed 90

I got my code to work but now I need to divide it into functions input(), processing() and output().

lista=[]
lista = [int(clan) for clan in input("Unesi članove niza : ").split(',')]
lista.reverse()
rezultat=[]
c=0
for i in lista:
    if i < 0:
        i = i * -1
        t = i
        rev = 0
        rev = rev * 10 + t % 10
        t = t // 10
        i = i * -1
        rezultat.append(str(i))
    else:
            t = i
            rev = 0
    while t > 0:
        rev = rev * 10 + t % 10
        t = t // 10
        if rev == i:
            c=c+1
            rezultat.append(str(i))
            if c == 0:
                print("")
print(','.join(rezultat))

I do not really know how to do it so it would be nice if someone can help me

3 Answers

Something like this...

def input(clan):

    lista = [int(clan) for clan in input("Unesi članove niza : ").split(',')]
    lista.reverse()

    return lista   


def processing(lista):

    rezultat = []
    c = 0
    for i in lista:
        if i < 0:
            i = i * -1
            t = i
            rev = 0
            rev = rev * 10 + t % 10
            t = t // 10
            i = i * -1
            rezultat.append(str(i))
        else:
                t = i
                rev = 0
        while t > 0:
            rev = rev * 10 + t % 10
            t = t // 10
            if rev == i:
                c=c+1
                rezultat.append(str(i))
                if c == 0:
                    print("")
    return(','.join(rezultat))

def output(result):

    print(result)

if __name__ == '__main__':

    result_list = input(clan)
    result = processing(result_list)
    output(result)

Try using the def command,
like this

def inp(): #declare a function
    #your commands
inp() #run the commands

Don't forget to declare global variables a.k.a. those variable that you might alter in many functions by using the global command.

try this

def get_input():
    lista = [int(clan) for clan in input("Unesi članove niza : ").split(',')]
    lista.reverse()
    return lista
    
def process_input(input):
    rezultat=[]
    c=0
    for i in input:
        if i < 0:
            i = i * -1
            t = i
            rev = 0
            rev = rev * 10 + t % 10
            t = t // 10
            i = i * -1
            rezultat.append(str(i))
        else:
            t = i
            rev = 0
        while t > 0:
            rev = rev * 10 + t % 10
            t = t // 10
            if rev == i:
                c=c+1
                rezultat.append(str(i))
                if c == 0:
                    print("")
                    
        return rezultat
        
def main():
    lista = get_input()
    result = process_input(lista)
    
    def output(xlist):
        return ','.join(xlist)
    
    output(result)

if __name__ == "__main__":
    main()

that should work, but I don't see the need to divide it into functions, since the code might even work well as one function.

Related