Exercise: Format some string information to look like so:
John Doe
123 Main Street
AnyCity, AS 09876

first='John'
last= 'Doe'
street='Main Street'
number= 123
city = 'AnyCity'
state='AS'
zipcode='09876'

print('{0} {1}\n{2} {3}\n{4}, {5} {6}'.format(first,last,number, street, city, state, zip code))

Exercise: Write a function that will take a list of data (last, first, class, average grade) and output formatted text in columns.

My Solution: I did a bit of trial and error in the IDE to get the text to line up correctly.

students = []
def roster(*x):
    print('Last                First               Class               Average Grade')
    for i in students:
        print('{0:20}{1:20}{2:20}{3:20}'.format(i[0],i[1],i[2],i[3]))

When a user adds to the list like so:

students.append(['DeMoines','Jim','Sophmore','3.45'])
students.append(['Backer','Bill','Junior','2.45'])
students.append(['Hill','Jack','Freshman','4.00'])
students.append(['Mary','Mary','Sophmore','3.23'])

This will return the following output:

Last                First               Class               Average Grade
DeMoines            Jim                 Sophmore            3.45                
Backer              Bill                Junior              2.45                
Hill                Jack                Freshman            4.00                
Mary                Mary                Sophmore            3.23  

Exercise: Make a function that takes a multi word string and turns it into an acronym.

My Solution: I decided to split the words in a sentence on the space between each word. This puts the words into list (x_list.) Then I loop over the elements in the list and add the first letter of each word in the x_list, into a new list called ac (upper casing the letters as well.) Finally I turn the ac list into a string, which is output.

def acronym(x):
    x_list = x.split(" ")
    ac = []
    for y in x_list:
        ac.append(y[0].upper())
    print(''.join(ac))

acronym("This is a test")

The join syntax still bugs me. It seems more logical to write: ac.join(”) rather then ”.join(ac)

#

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *

Archives
Categories