Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Numbering Show list

How would you set it up so when the user types SHOW the list appears number? For example 1. Eggs 2. Bacon 3. Milk

Thanks

1 Answer

Josh Keenan
Josh Keenan
19,652 Points

Here's a quick example I made:

my_list = ["James", "Jessica", "Emily", "Rebecca", "Tom"]

def show(a_list):
  count = 1
  for i in a_list:
    print("{}. {}".format(count, i))
    count += 1

show(my_list)

The function is taking a list as a parameter, then with the list it starts a for loop to iterate over it, then it just prints a string with the count and the item in the list formatted into it. Count is incremented each iteration and is initialised at one so it is in a normal format. Hope this makes sense