As we can see from the design that we’ve laid out at the beginning of this project, we’ll need to print out a border on the top and bottom. Let’s start with the top border:
14| # create a top border
15| print( "*" * 50 )
Go ahead and run the cell. There’s a new concept being applied here, where we write “*” * 50. All we’re trying to do is print out 50 stars in a row for a top border, and rather than making 50 print statements, we can simply multiply the string by the number we want. This way we get our top border while keeping our code slim and easy to read. Readability of code is always key.
Displaying the Company Info
We’ve already defined our variables for the company in the preceding lines, so let’s display them:
17| # print company information first, using format
18| print( "\t\t{ }".format( company_name.title( ) )
19| print( "\t\t{ }".format(company_address) )
20| print( "\t\t{ }".format(company_city) )
Go ahead and run the cell. These print statements may seem a little hard to understand at first; however, I’m introducing an escape character to you. Escape characters are read in by the defining backslash “\” character. Whatever comes after that backslash is what the computer will interpret. In the three print statements, we use “\t” for a tab indentation. Another popular escape character you may see is “\n” which means newline and acts as if you hit the enter key. We use two escaping characters in a row to center it within our output. Let’s create a divider:
22| # print a line between sections
23| print( "=" * 50 )
Go ahead and run the cell. Like how we printed out our top border, we’ll multiply the equal symbol by 50 to create the same width line. This will give the appearance of separate sections.
Displaying the Product Info
Looking at our original design, we want to create a header before we list out each product’s name and price. This can be done simply by using our escaping characters for indenting:
25| # print out header for section of items
26| print("\tProduct Name\tProduct Price")
Go ahead and run the cell. Due to the size of the header names, we only need to use a single tab before each header. Now we can go ahead and output a row for each products’ information:
28| # create a print statement for each product
29| print( "\t{ }\t\t${ }".format(p1_name.title( ), p1_price) )
30| print( "\t{ }\t\t${ }".format(p2_name.title( ), p2_price) )
31| print( "\t{ }\t\t${ }".format(p3_name.title( ), p3_price) )
Go ahead and run the cell. We’re using similar styles as the previous print statements in order to center each product’s title and price under their respective headers. Try not to get too confused by all the symbols within the print string; you can simply break them down to a tab, followed by the first variable being formatted into the string, followed by two tabs, followed by a dollar sign (in order to make the price look like currency), and followed by the second variable being formatted into the string. This completes the section for our items, so let’s put in another section divider:
33| # print a line between sections
34| print('=' * 50)
Go ahead and run the cell. This will set us up for our next section to display the total.
Do'stlaringiz bilan baham: |