Is this a good implementation of functions in Python?

Viewed 29

I'm relatively new to Python and am using it in an App Development course I'm taking. Below is code that I wrote as part of my first assignment, which was to print a name in a variety of ways. I decided to create functions for every variation that my program uses to print out a name, and call them using a main function.

Is this a good practice, or simply overkill? I opted to utilize functions to ensure reusability, but I'm not sure if there's a better solution to provide reusability.

fnameinput=input("Enter your name: ")
lnameinput=input("Enter your last name: ")

#Extra space at the end is so that vertical/reversevertical function outputs are separated
lnamelist=list(lnameinput+" ")
fnamelist=list(fnameinput+" ")

reversefnamelist=fnamelist[::-1]
reverselnamelist=lnamelist[::-1]

def intro():
    #Function that prints Hello World and It's nice to meet you on a separate line
    return print("Hello World!\nIt's nice to meet you!")

def verticalfname():
    #Function that prints list fnamelist, only that each token (part of string) is separated by a newline escape sequence (sep="\n").
    return print(*fnamelist, sep="\n")

def reverseverticalfname():
    #Function with the same behavior as verticalname, but it reverses the list order and prints out the tokens in a reverse order.
    return print(*reversefnamelist, sep="\n")

def reverseverticallname():
    #Same behavior as function reverseverticalname except it reverses list reverselnamelist.
    return print(*reverselnamelist, sep="\n")

def concat():
    #Function concatenates the fnameinput and lnameinput strings read in.
    return print(fnameinput+" "+lnameinput)

def onelinename():
    #Function that prints the strings read in using two print statements. Not that a return statement is not present due to its exclusivity in a function.
    print(fnameinput)
    print(lnameinput,"\n")

def firstname():
    #Function that returns the read first name back. Used placeholder concatenation (%s) rather than using a plus sign/comma.
    return print("Nice to meet you, %s!"%(fnameinput))

def main():
    #Main function containing all of the previous functions written.
    intro()
    verticalfname()
    reverseverticalfname()
    reverseverticallname()
    concat()
    onelinename()
    firstname()

if __name__ == "__main__":
    #Conditional that runs the main function, which in turn runs all of the other functions contained within it.
    main()    
1 Answers

While this does lean heavily into opinion territory, especially regarding the use of global variables vs. function parameters/arguments, there is one thing that can be observed about what you're doing that is not opinion.

print returns None, so there's no effective difference between:

def hw():
  return print("Hello world!")

And:

def hw():
  print("Hello world!")
Related