Search code examples
pythonfor-loopindexingformatperiod

How do I get periods to display behind the index numbers?


I'm working on a project for school. I was able to get my function to work the way I need it to, using all of the parameters that the teacher wanted, except when it displays. I need it to pull from two lists, which it does, and display both lists next to each other, which it does. However in the teacher's version, the display/output looks like this:

0.  ($2.39)   Drip coffee
1.  ($3.59)   Hot chocolate
2.  ($3.79)   Caffe Latte
3.  ($1.49)   Bagel
4.  ($2.69)   Blueberry muffin
5.  ($2.39)   Chocolate chip cookie

6. Reset order
7. Checkout

However, my version looks like this:

0  ($2.39)   Drip coffee
1  ($3.59)   Hot chocolate
2  ($3.79)   Caffe Latte
3  ($1.49)   Bagel
4  ($2.69)   Blueberry muffin
5  ($2.39)   Chocolate chip cookie

6. Reset order
7. Checkout

As you can see, I am missing the periods after the index numbers (Sorry if my terminology is not accurate. I am an extreme beginner and I'm having a hard time keeping the terms straight.). Here is my code:

products = [    "Drip coffee",
                "Hot chocolate",
                "Caffe Latte",
                "Bagel",
                "Blueberry muffin",
                "Chocolate chip cookie" ]

prices = [      2.39,
                3.59,
                3.79,
                1.49,
                2.69,
                2.39 ]

for i, product in enumerate(products):
  price = prices[i]
  print(i, "(${})".format(price), product)
print()
print("6. Reset", "\n" + "7. Checkout")

Is there any way I can add to this or fix it so that the output will display periods in the places I need them? Also, the instructions for the project say to place the options for 6 and 7 as a string outside of the for loop. That's why they are not included like the others. That's also why I was easily able to add the period behind those last two numbers lol. Any help would be appreciated as well as an explanation as to why the solution is what it is. Thanks, everyone!


Solution

  • In order to print a period, you have a tell python to print a period, of course!

    print("{}.".format(i), "(${})".format(price), product)
    

    However, that's a pretty funky print statement. Let's explore a few other options:

    # a bit more readable using format
    print("{}. (${}) {}".format(i, price, product))
    # using the old-style string formatting
    print("%d. ($%.2f) %s" % (i, price, product))
    # using new-style f-strings
    print(f"{i}. (${price:.2f}) {product}")
    

    For reference, old-style string formatting and f-strings. Also, here's a cheat sheet for string formatting. I've used .2f to indicate a fixed point number with precision 2, so e.g. 2.5 prints as 2.50, and 2.504234 also prints as 2.50.

    Also, let's make your loop a little more pythonic with zip:

    for i, (prod, price) in enumerate(zip(products, prices)):
       print(f"{i}. (${price:.2f}) {prod}")
    

    Much cleaner, no? Have fun learning!