Jump to content

Python Help

Go to solution Solved by Linus_torvalds,

Do you know how I would implement that in this case???

here it works :)

def border_msg(msg):    split_msg = None    longest = 0    border = None    message = None    mylist = []        split_msg = msg.split('\n')        for i in split_msg:    	mylist.append(i)        longest = len(max(mylist, key=len))        border = '+%s+\n' %('-'*(longest + 2))    message = border        for i in split_msg:        message += '| %s |\n' % i.center(longest, ' ')        message += border        return message    print(border_msg('hello there\nmy name is nick'))

Hey guys I have to write a program that when given a string does this:

+-­‐-­‐-­‐-­‐-­‐-­‐-­‐+|    hello     |+-­‐-­‐-­‐-­‐-­‐-­‐-­‐+

this is my code so far:

def border_msg(msg):    split_msg = None    longest = 0    border = None    message = None        split_msg = msg.split('\n')        for i in split_msg:        if len(i) > longest:            longest = len(i)        border = '+%s+\n' %('-'*(longest + 2))    message = border        for i in split_msg:        message += '| %s |\n' % i        message += border        return message    print(border_msg('hello there\nmy name is nick'))

But my code does this:

+-----------------+| hello there || my name is nick |+-----------------+

How can I center that first line of text? Thanks so much for your help!

Link to comment
https://linustechtips.com/topic/465679-python-help/
Share on other sites

Link to post
Share on other sites

Do you know how I would implement that in this case???

here it works :)

def border_msg(msg):    split_msg = None    longest = 0    border = None    message = None    mylist = []        split_msg = msg.split('\n')        for i in split_msg:    	mylist.append(i)        longest = len(max(mylist, key=len))        border = '+%s+\n' %('-'*(longest + 2))    message = border        for i in split_msg:        message += '| %s |\n' % i.center(longest, ' ')        message += border        return message    print(border_msg('hello there\nmy name is nick'))

Computer users fall into two groups:
those that do backups
those that have never had a hard drive fail.

Link to comment
https://linustechtips.com/topic/465679-python-help/#findComment-6268783
Share on other sites

Link to post
Share on other sites

 

 

here it works :)

def border_msg(msg):    split_msg = None    longest = 0    border = None    message = None    mylist = []        split_msg = msg.split('\n')        for i in split_msg:    	mylist.append(i)        longest = len(max(mylist, key=len))        border = '+%s+\n' %('-'*(longest + 2))    message = border        for i in split_msg:        message += '| %s |\n' % i.center(longest, ' ')        message += border        return message    print(border_msg('hello there\nmy name is nick'))

Thanks!!! I unfortunately handed in the assignment already but thanks!

Link to comment
https://linustechtips.com/topic/465679-python-help/#findComment-6283826
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×