BYU logo Computer Science

Bullet Styles Transcript

[00:00:00] Hey there. I’m going to walk you through the bullet styles activity. To help us get a feel for lists and how we use them, how we build them up, how we iterate over the pieces.

Start visual description. Screen reads:
Bullet styles
Write a program that:

  1. Asks the user for a series of items
  2. Print the list of items using the following bullets: •, -, >

End visual description.

[00:00:14] So here’s the problem, right. We want to write a program that’s going to ask the user for a series of items and then the user will input q when they’re done inputting items and then we’re going to print that list of items with the following bullets, right. So that’s an asterisk, a dash, and a greater than sign.

[00:00:34] So with all programming problems the very first step again is decomposing the problem. What are the big pieces that we want to approach? And here I see two basic steps, right. We’re going to ask the user for input and then we’re going to print the items out.

[00:00:53] So let’s start our bullet styles dot py script and let’s just sketch out those two pieces. Let’s come in here. When I have a blank piece of paper, blank editor, to write a program I will always start with just defining a main method and then our if name equals main block, right. So I will say def main, let’s just say pass if name equals main, call main. You can call this whatever you want.

[00:01:33] I use main just out of habit in other programming languages like Java or c++, it has to be called main. So I just call it main. It’s my main function but you can call it whatever you want. And so in here we have two basic ideas right we need to get a list of items from the user and then we need to print those items with bullets.

[00:02:12] And in this case, let’s just do this: items equals get items. And assuming that get items gets them from the user and returns them to us we’ve got them now, great. And then we want to print this out right. Print items.

[00:02:26] So now we just need to define these two functions and we’re all set. So here we’re just going to ask…there’s no specific instructions about what the prompt should look like or anything else. When you’re working with an auto grader we’ll provide very specific instructions because you need to be able to match the expected output.

[00:02:45] In this case, we could be creative in how we want to do it. So I’m going to create a function get items. Right. Queries the user for a list of items, one at a time. So while true, let’s actually come up here. So if we’re going to create a list, to build up a list, we need to start with an empty list, right? So let’s start with items equals empty list and at the very end we want to return those items.

[00:03:32] So just creating a little shell here of what this function’s supposed to, now we need to fill in these details so that we’re actually getting things from the user, right. So we’re going to say, you know item equals input item. Why not? If that item is actually the letter q you want to break.

[00:03:59] Otherwise, we want to say items dot append item. So we’re going to ask the user for an item, they’re going to give it to us. If they told us q, then we’re done. But if they didn’t tell us q, they told us some kind of item we’re going to stick that in our list of items, and then once that loop is finished, once we break out of that loop, we’re going to return the items and we’re done.

[00:04:28] Now I have the items, so I just need to define what print items means, the function print items. For now, let’s just do something like this: print items. We’re supposed to print it out with the bullets, right? But to test what we have so far let’s just throw the print statement in and see that it’s working. I’m sorry, we’re implementing the wrong script.

Start visual description. Instructor types the following code:
def get_items():
"""Queries the user for a list of items, one at a time."""
items = []
while True:
item = input(‘Item: ‘)
if item == ‘q’:
else:
break
items.append(item)
return items
End visual description.

[00:04:56] We’ll put this in the bullets dot py file so here it’s querying for us item so we can say I don’t know lizard. And chair and bike quit. And then lizard, chair, and bike was the items that we got out of that. Perfect. So again, we got some items, quit, and then we printed those out: lizard, chair, bike. So now let’s print those items out with bullets and then test it again. Print items with the bullets star, dash, and greater than.

Start visual description. Text on console reads:
Item: Lizard Item: chair
Item: bike
Item: q
[‘lizard’, ‘chair’, ‘bike’]
Process finished with exit code 8
End visual description.

[00:05:46] Alright, so ultimately what do we want to have happen? Well we want print with bullets items the star. And then we would want to do the same thing with a dash. And the same thing with a greater than. Right? And that would be that. So what does print the bullets mean? Well let’s go define that.

[00:06:15] So using each item at a time, we need to display it right? So if we come back over here, something like this. Let’s see if I can’t draw on here. So we’re going to have like asterisk lizard, asterisk chair, asterisk bike. Right? And then repeat the process with dashes, repeat the process with greater thans.

Start visual description. Instructor types the following code:
def print items (items):
"""Print the items with the bullets , -,> “””
print_with_bullets(items, ’
‘)
print_with_bullets(items, ’-‘)
print_with_bullets(items, ’>‘)
End visual description.

[00:06:48] So we want to go through each item and just print that item out with the bullet sitting in the front of it, right? So let’s come back over here, for item in items. Again, this part, that’s a variable. We get to name it. What do you call a single element from a list of items? Well, item is a nice word. We could call it Cosmo. We could call it thing.

[00:07:14] Pick a variable name that’s helpful and intuitive, that helps you keep track of what the thing is. Right, so I’ve got some items. So for each item in that list, I’m going to want to print out something where, oh they called this one param. Let’s call this bullet.

[00:07:33] That helps me keep track a little bit better of what that’s supposed to be. Then we’re going to say bullet space item. That’s what I want to print. And that’s it. I just want to print out each one just like that.

[00:07:49] So let’s see what that looks like and if we want to make any adjustments to it. Item, again we need some items. Water bottle, desk, and light. Quit. And it’s printed out those, each of those items with the different bullets that we asked it to use.

Start visual description. Instructor types the following code:
def print_with_bullets (items, bullet):
for item in items:
print(f{bullet} {item}‘)
End visual description.

[00:08:17] Perfect. Now before we move on, could I have done more than three items? Sure, right, I could’ve done ten items. And that’s fine. I’m noticing here I’m doing the same line of code over and over and over with a different input.

[00:08:36] Well that concept, doing the same thing, just with a different input each time, that’s what iterating over lists is all about. We could change this to instead be something like: bullets equals a list where we have star, dash, greater than. For bullet in bullets print item with bullets, right? Pass the bullet in there.

[00:09:09] Now we’ve simplified the code where we can see we can define what bullets we want here and now for each bullet that we have we print it out. If we decide we want to include other bullets… what’s a good bullet? Maybe this one, sure why not? Question mark, why not? We just add them to that list and now automatically it’s going to do the print with bullets thing.

Start visual description. Instructor adds the following code the the print_items function:
bullets = [‘
’, ‘-’, ‘>’]
for bullet in bullets:
print_with_bullets(items, ‘’)
End visual description.

[00:09:38] For those new ones, you don’t have to add more lines where we’re calling print with bullets with another input. This already builds that in that way. Let’s try this out. So item: keyboard, laptop, monitor, mouse, cable, quit.

Start visual description. Instructor updates the code for print_items function to:
bullets = [‘
’, ‘-’, ‘>’, ‘<’, ‘?’]
for bullet in bullets:
print_with_bullets(items, bullet)
End visual description.*

[00:10:03] And now, what do we have right? Every one of those got printed out. That list with each of the different bullets that we specified. So that’s the power of lists. If you’re going to be doing the same thing each time but for a different item, then think lists and for each item in a list and then build your code around that. Alright I’ll see you in a minute on our next activity.