Chevron Left Icon
Lists and Functions
Lesson 1 of 2
Chevron Right Icon
  1. 1
    This exercise goes over just pulling information from a list, which we’ve covered in a previous section!
  2. 2
    You’ve already learned how to modify elements of a list in a previous section. This exercise is just a recap of that!
  3. 3
    Here, we’ll quickly recap how to .append() elements to the end of a list.
  4. 4
    This exercise will expand on ways to remove items from a list. You actually have a few options. For a list called n: 1. n.pop(index) will remove the item at index from the list and return it to y…
  5. 5
    In this exercise, you will just be making a minor change to a function to change what that function does.
  6. 6
    This exercise will recap how to use more than one argument in a function.
  7. 7
    This is a basic recap on using strings in functions.
  8. 8
    You pass a list to a function the same way you pass any other argument to a function.
  9. 9
    Passing a list to a function will store it in the argument (just like with a string or a number!) def first_item(items): print items[0] numbers = [2, 7, 9] first_item(numbers) 1. In the examp…
  10. 10
    Modifying an element in a list in a function is the same as if you were just modifying an element of a list outside a function. def double_first(n): n[0] = n[0] * 2 numbers = [1, 2, 3, 4] doubl…
  11. 11
    You can also append or delete items of a list inside a function just as if you were manipulating the list outside a function. my_list = [1, 2, 3] my_list.append(4) print my_list # prints [1, 2, 3,…
  12. 12
    This exercise will go over how to utilize every element in a list in a function. You can use the existing code to complete the exercise and see how running this operation inside a function isn’t mu…
  13. 13
    This exercise shows how to modify each element in a list. It is useful to do so in a function as you can easily put in a list of any length and get the same functionality. As you can see, len(n) is…
  14. 14
    Okay! Range time. The Python range() function is just a shortcut for generating a list, so you can use ranges in all the same places you can use lists. range(6) # => [0, 1, 2, 3, 4, 5] range(1, 6)…
  15. 15
    Now that we’ve learned about range, we have two ways of iterating through a list. Method 1 - for item in list: for item in list: print item Method 2 - iterate through indexes: for i …
  16. 16
    Now let’s try working with strings! for item in list: print item for i in range(len(list)): print list[i] The example above is just a reminder of the two methods for iterating over a list.
  17. 17
    Using multiple lists in a function is no different from just using multiple arguments in a function! a = [1, 2, 3] b = [4, 5, 6] print a + b # prints [1, 2, 3, 4, 5, 6] The example above is just…
  18. 18
    Finally, this exercise shows how to make use of a single list that contains multiple lists and how to use them in a function. list_of_lists = [[1, 2, 3], [4, 5, 6]] for lst in list_of_lists: fo…
  1. 1
    In this project you will build a simplified, one-player version of the classic board game Battleship! In this version of the game, there will be a single ship hidden in a random location on a 5x5 …
  2. 2
    The first thing we need to do is to set up the game board.
  3. 3
    Good! Now we’ll use a built-in Python function to generate our board, which we’ll make into a 5 x 5 grid of all “O”s, for “ocean.” print [“O”] * 5 will print out [‘O’, ‘O’, ‘O’, ‘O’, ‘O’], which…
  4. 4
    Great job! Now that we’ve built our board, let’s show it off. Throughout our game, we’ll want to print the game board so that the player can see which locations they have already guessed. Regular…
  5. 5
    Now we can see the contents of our list, but clearly it would be easier to play the game if we could print the board out like a grid with each row on its own line. We can use the fact that our b…
  6. 6
    We’re getting pretty close to a playable board, but wouldn’t it be nice to get rid of those quote marks and commas? We’re storing our data as a list, but the player doesn’t need to know that! let…
  7. 7
    Excellent! Now, let’s hide our battleship in a random location on the board. Since we have a 2-dimensional list, we’ll use two variables to store the ship’s location, ship_row and ship_col. fro…
  8. 8
    Good job! For now, let’s store coordinates for the ship in the variables ship_row and ship_col. Now you have a hidden battleship in your ocean! Let’s write the code to allow the player to guess w…
  9. 9
    Awesome! Now we have a hidden battleship and a guess from our player. In the next few steps, we’ll check the user’s guess to see if they are correct. While we’re writing and debugging this par…
  10. 10
    Okay—now for the fun! We have the actual location of the ship and the player’s guess so we can check to see if the player guessed right. For a guess to be right, guess_col should be equal to ship…
  11. 11
    Great! Of course, the player isn’t going to guess right all the time, so we also need to handle the case where the guess is wrong. print board[2][3] The example above prints out “O”, the elemen…
  12. 12
    Great job! Now we can handle both correct and incorrect guesses from the user. But now let’s think a little bit more about the “miss” condition. 1. They can enter a guess that’s off the board. 2…
  13. 13
    Great! Now let’s handle the second type of incorrect guess: the player guesses a location that was already guessed. How will we know that a location was previously guessed? print board[guess_row…
  14. 14
    Congratulations! Now you should have a game of Battleship! that is fully functional for one guess. Make sure you play it a couple of times and try different kinds of incorrect guesses. This is…
  15. 15
    You can successfully make one guess in Battleship! But we’d like our game to allow the player to make up to 4 guesses before they lose. for turn in range(4): # Make a guess # Test that guess …
  16. 16
    If someone runs out of guesses without winning right now, the game just exits. It would be nice to let them know why. Since we only want this message to display if the user guesses wrong on their …
  17. 17
    Almost there! We can play Battleship!, but you’ll notice that when you win, if you haven’t already guessed 4 times, the program asks you to enter another guess. What we’d rather have happen is for …
  18. 18
    Congratulations! You have a fully functional Battleship game! Play it a couple of times and get your friends to try it out, too. (Don’t forget to go back and remove the debugging output that gives …
  19. 19
    You can also add on to your Battleship! program to make it more complex and fun to play. Here are some ideas for enhancements—maybe you can think of some more! 1. Make multiple battleships: you’ll…

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo