HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Name Banner Program

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
bannernameprogram

Problem

I have a program that takes an input from the user (the user's name) and print a result with their name inside a border such as:

*************
* name_here *
*************


The code:

def style_1():
    print "*" * (length_of_name + 4)
    print "*", name, "*"
    print "*" * (length_of_name + 4)

def style_2():
    print "#" * (length_of_name + 4)
    print "#", name, "#"
    print "#" * (length_of_name + 4)

def style_3():
    print "$" * (length_of_name + 4)
    print "$", name, "$"
    print "$" * (length_of_name + 4)

def style_4():
    print "^" * (length_of_name + 4)
    print "^", name, "^"
    print "^" * (length_of_name + 4)

def style_5():
    print ":" * (length_of_name + 4)
    print ":", name, ":"
    print ":" * (length_of_name + 4)

def style_selection():
    if style == "*":
        style_1()
    elif style == "#":
        style_2()
    elif style == "$":
        style_3()
    elif style == "^":
        style_4()
    elif style == ":":
        style_5()
    elif style == "EXIT":
        print "Thank you for using the Name Banner Program!"
        exit()

while 1:
    name = raw_input("What is your name: ")
    style = raw_input("What is your style (*, #, $, ^, :): ")
    length_of_name = len(name)
    style_selection()


Is there anything to do to shorten the code and/or improve its efficiency?

Solution

Yes, you can pass the style directly:

def frame(name, style_character='*'):
    """Frame the name with the style_character."""

    frame_line = style_character * (len(name) + 4)
    print(frame_line)
    print('{0} {1} {0}'.format(style_character, name))
    print(frame_line)


Please notice how I've eluded the need for doing the length outside of the function, and not using globals. I've also used the new style of print formatting.

You do however need to change your main code a little bit, into something like:

if __name__ == '__main__':

    frame("holroy")

    while True:
        name = raw_input('Please enter name (or "quit"): ')
        if name == 'quit':
            break

        style_character = raw_input('Enter frame character: ')

        frame(name, style_character)


Please don't use 1 instead of True, that is an anti-pattern. Also use if __name_ ... construct, as this also allows for your code to used a module from another script by simply importing it, and doing modulename.frame('Your name')

The output could now be like the following:


holroy


Please enter name (or "quit"): PythonMaster

Enter frame character: #
################
# PythonMaster #
################

Please enter name (or "quit"): quit


If you break out of a loop at the right place, there is also no need to call exit() explicit.

Code Snippets

def frame(name, style_character='*'):
    """Frame the name with the style_character."""

    frame_line = style_character * (len(name) + 4)
    print(frame_line)
    print('{0} {1} {0}'.format(style_character, name))
    print(frame_line)
if __name__ == '__main__':

    frame("holroy")

    while True:
        name = raw_input('Please enter name (or "quit"): ')
        if name == 'quit':
            break

        style_character = raw_input('Enter frame character: ')

        frame(name, style_character)

Context

StackExchange Code Review Q#111150, answer score: 8

Revisions (0)

No revisions yet.