简体   繁体   English

如何使用函数 Python 3.x 划分代码

[英]How to divide code using functions Python 3.x

I got my code to work but now I need to divide it into functions input(), processing() and output().我让我的代码可以工作,但现在我需要将它分成函数 input()、processing() 和 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我真的不知道该怎么做所以如果有人可以帮助我会很好

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,尝试使用 def 命令,
like this像这样

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

Don't forget to declare global variables aka those variable that you might alter in many functions by using the global command.不要忘记声明全局变量,也就是您可以使用global命令在许多函数中更改的变量。

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.这应该可行,但我认为没有必要将其划分为函数,因为代码甚至可能像 function 一样运行良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM