CMSC 201 Spring 2019 Lab 06 Lists

Similar documents
CMSC 201 Spring 2017 Lab 05 Lists

CMSC 201 Fall 2018 Lab 04 While Loops

CMSC 201 Spring 2017 Homework 4 Lists (and Loops and Strings)

CMSC 201 Fall 2016 Homework 6 Functions

CMSC 201 Fall 2015 Lab 12 Tuples and Dictionaries

CMSC 201 Spring 2017 Lab 12 Recursion

CMSC 201 Spring 2016 Lab 04 For Loops

CMSC201 Computer Science I for Majors

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

CMSC201 Computer Science I for Majors

CMSC 201 Spring 2018 Project 3 Minesweeper

CMSC 201 Spring 2018 Project 2 Battleship

CMSC 201 Spring 2017 Project 1 Number Classifier

CMSC 201 Spring 2016 Lab 08 Strings and File I/O

CMSC 201 Fall 2016 Lab 13 More Recursion

CMSC 201 Spring 2016 Homework 7 Strings and File I/O

CMSC 201 Spring 2017 Lab 01 Hello World

CMSC 201 Spring 2018 Lab 01 Hello World

CS 1803 Pair Homework 4 Greedy Scheduler (Part I) Due: Wednesday, September 29th, before 6 PM Out of 100 points

CMPSCI 187 / Spring 2015 Hanoi

Text Input and Conditionals

CMSC201 Computer Science I for Majors

CS 2316 Individual Homework 4 Greedy Scheduler (Part I) Due: Wednesday, September 18th, before 11:55 PM Out of 100 points

CS 051 Homework Laboratory #2

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

CMSC 201 Spring 2018 Lab 13 Dictionaries

CISC 181 Lab 2 (100 pts) Due: March 4 at midnight (This is a two-week lab)

LOOPS. Repetition using the while statement

Introduction to Programming with JES

1. Introduction EE108A. Lab 1: Combinational Logic: Extension of the Tic Tac Toe Game

CMSC201 Computer Science I for Majors

APPM 2460 Matlab Basics

CSCI 1100L: Topics in Computing Lab Lab 1: Introduction to the Lab! Part I

Using the Zoo Workstations

Animations involving numbers

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

CMPSCI 187 / Spring 2015 Sorting Kata

Homework 5: Fun with Scala Due: 5:00 PM, Mar 16, 2018

Valuable points from Lesson 6 Adobe Flash CS5 Professional Classroom in a Book

More About WHILE Loops

If Statements, For Loops, Functions

CS 100: Computability, Python Lists

GEO 425: SPRING 2012 LAB 9: Introduction to Postgresql and SQL

ECS Baruch Lab 3 Spring 2019 Name

Self-Teach Exercises: Getting Started Turtle Python

Project 1 Balanced binary

CMSC201 Computer Science I for Majors

Python lab session 1

CS 1110, LAB 10: ASSERTIONS AND WHILE-LOOPS 1. Preliminaries

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Programming in the Real World. Dr. Baldassano Yu s Elite Education

CSCI 141 Computer Programming I. Filip Jagodzinski

Decisions, Decisions. Testing, testing C H A P T E R 7

CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

CS 1301 Individual Homework 3 Conditionals & Loops Due: Monday February 8 th before 11:55pm Out of 100 points

Table of Laplace Transforms

Pattern Maker Lab. 1 Preliminaries. 1.1 Writing a Python program

1 Lecture 5: Advanced Data Structures

CS 201 Advanced Object-Oriented Programming Lab 6 - Sudoku, Part 2 Due: March 10/11, 11:30 PM

Part 6b: The effect of scale on raster calculations mean local relief and slope

Animations that make decisions

Homework 5. Due Friday, March 1 at 5:00 PM

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

ASYMPTOTIC COMPLEXITY

Spring CS Homework 12 p. 1. CS Homework 12

Loop structures and booleans

Smoother Graphics Taking Control of Painting the Screen

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

CMSC201 Computer Science I for Majors

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

CS Homework 11 p. 1. CS Homework 11

My First iphone App (for Xcode version 6.4)

XP: Backup Your Important Files for Safety

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

Lab 1: Silver Dollar Game 1 CSCI 2101B Fall 2018

CMSC162 Intro to Algorithmic Design II Blaheta. Lab March 2019


CS1 Lecture 9 Feb. 5, 2018

Homework 7: Subsets Due: 11:59 PM, Oct 23, 2018

Homework: More Abstraction, Trees, and Lists

COP4530 Data Structures, Algorithms and Generic Programming Recitation 3 Date: January 20 & 22, 2009

(edit 3/7: fixed a typo in project specification 2-f) user_id that user enters should be in the range [0,n-1] (i.e., from 0 to n-1, inclusive))

Lesson 2. Introducing Apps. In this lesson, you ll unlock the true power of your computer by learning to use apps!

Lab 9: Pointers and arrays

CMSC201 Computer Science I for Majors

Homework 2: Imperative Due: 5:00 PM, Feb 15, 2019

CSC 360 Lab Assignment #6 Spring 2015 Due: March 13, 2015

Implement an ADT while using Subversion

CMPSCI 187 / Spring 2015 Hangman

Adding content to your Blackboard 9.1 class

Programming Project 1

CS50 Supersection (for those less comfortable)

Spring CS Homework 3 p. 1. CS Homework 3

Homework # 7 DUE: 11:59pm November 15, 2002 NO EXTENSIONS WILL BE GIVEN

CS4023 Week04 Lab Exercise

Math 15 - Spring Homework 5.2 Solutions

Lab 7: OCaml 12:00 PM, Oct 22, 2017

Transcription:

CMSC 201 Spring 2019 Lab 06 Lists Assignment: Lab 06 Lists Due Date: Thursday, March 7th by 11:59:59 PM Value: 10 points This week s lab will put into practice the concepts you learned about lists: indexing, mutating, and traversing. It will also make use of while loops, both to get input from the user, and to traverse the contents of the list. (Having concepts explained in a new and different way can often lead to a better understanding, so make sure to pay attention as your TA explains.) CMSC 201 Computer Science I for Majors Page 1

Part 1A: Review Sentinel While Loops One way to use a while loop is as a sentinel loop. A sentinel loop continues to process data until reaching a special value that signals the end of the data. The special value is called the sentinel. Here is the pseudocode for a sentinel loop in Python: Get the first data item from the user While data item is not the sentinel Process the data item Get the next data item from the user One of the scenarios in which we can implement this type of loop is a version of our grocery list program that allows us to enter as many items as we like. Although it is similar to previous versions, the interactive (sentinel) while loop of the grocery list program allows us to enter as many items as we like until the sentinel value of "exit" is entered. SENTINEL = "exit" def main(): # initialize the list to be empty grocery_list = [] # get the initial user value msg = "Enter an item, or '" + SENTINEL + "' to end: " userval = input(msg) main() # run the while loop until the user enters "exit" while userval!= SENTINEL: grocery_list.append(userval) # get another value from the user userval = input(msg) print("remember to buy", grocery_list) CMSC 201 Computer Science I for Majors Page 2

Part 1B: Review Lists and Indexing Lists are an easy way to hold lots of individual pieces of data without needing to make lots of variables. They are a type of data structure, which are specialized ways of organizing and storing data. In order to get a specific variable, or element, from a list, we need to access that index of the list. NOTE: Lists don t starting counting from 1 the first element in the list is at index 0. For example, the following line of code creates a list called names: names = ["Aya", "Brad", "Carlos", "David", "Emma"] Which creates the list (called names) below: Aya Brad Carlos David Emma 0 1 2 3 4 CMSC 201 Computer Science I for Majors Page 3

Part 1C: Review Traversing Lists Looking at the contents of a list is also known as traversing the list, and can be done using a basic while loop. In the loop, we use a variable to keep track of which item in the list we are looking at by having it store the index of that item. As we move on to the next item, that variable is incremented, until we reach the end of the list. The length of the list is an important property, as it is used to tell the while loop when to stop traversing the list. The length can be gotten by using the len() function. For example, this code would traverse the names list above, printing out that each person is awesome: # this variable can be called anything # it starts at zero because that's the first index index = 0 while index < len(names): print(names[index], "is awesome!") index += 1 CMSC 201 Computer Science I for Majors Page 4

Part 1D: Review Mutating Lists Lists can also be mutated we can add and remove items from them as many times as we want. This means that we can start off with an empty list (denoted as two square brackets: newlist = []) and fill it as necessary. Adding to a list is easy to do: simply add the new item to the end of the list, using the.append() function. The following line of code adds a few items to a list called newlist: newlist.append("a Thing") newlist.append(1.37) newlist.append(0) newlist.append(false) After we run these lines of code, our list would look like this: "A Thing" 1.37 0 False 0 1 2 3 To remove items from the list, we use the appropriately named.remove() function. The.remove() function takes in what we want to remove, not where it is in the list. For example, if we call it and ask it to remove 0, it will remove the third element, the integer 0, and not the string "A Thing", which is stored at index 0. newlist.remove(0) "A Thing" 1.37 False 0 1 2 The.remove() function also updates the indexes of anything after the removed element, so that our list looks like a regular list after the element was deleted. (In other words, notice how the index at which False is stored changes from 3 before the removal to 2 afterwards.) CMSC 201 Computer Science I for Majors Page 5

Part 2: Exercise In this lab, you ll be creating one file, games.py, but you ll be creating it in four steps. That way, you can focus on each of the steps needed one by one. The program you ll be coding will display different options for games, and will allow users to vote on their favorite, using the numbers printed next to each one. Once voting is over, your program will print out each game and how many votes it earned. Tasks Create a games.py file Write the code to print out the game choices Write the code to get votes from the user Write the code to save the votes in a list Write the code to print out the total votes (You should run and test your games.py file after each step) Submit completed games.py file CMSC 201 Computer Science I for Majors Page 6

Part 3A: Creating Your File First, create the lab05 folder using the mkdir command -- the folder needs to be inside your Labs folder as well. (If you need a reminder of how to create and navigate folders, try asking a classmate next to you for help. If you re both stuck, ask the TA or refer to the instructions for Lab 1.) Next, create a Python file called games.py using the touch command in GL. The touch command creates a new blank file, but doesn t open it. Once a file has been touched, you can open and edit it using emacs. touch games.py emacs games.py The first thing you should do with any new Python file is create and fill out the comment header block at the top of your file. Here is a template: # File: FILENAME.py # Author: YOUR NAME # Date: TODAY'S DATE # Section: YOUR SECTION NUMBER # E-mail: USERNAME@umbc.edu # Description: YOUR DESCRIPTION GOES HERE AND HERE # YOUR DESCRIPTION CONTINUED SOME MORE CMSC 201 Computer Science I for Majors Page 7

Part 3B: Printing the Game Choices This is the first of four steps that must be written for this lab. The first step is to copy in the list of games, and to write code that will print out the different choices. Copy the list below into your program s main(): # list of games to vote on games = ["Twister", "Dodgeball", "Capture the Flag", "Hide and Seek", "Croquet", "Ring Toss", "Ping Pong"] To print the choices, you should write a while loop that will print out the following two things on each line: The number of the choice (with the count starting at 1, not 0!) The game s name Here is some sample output for this part of the program. (Yours should match this word for word.) linux1[5]% python games.py 1 - Twister 2 - Dodgeball 3 - Capture the Flag 4 - Hide and Seek 5 - Croquet 6 - Ring Toss 7 - Ping Pong Once this part of the program works correctly, move on to the next step. Having trouble making the numbering start at 1 instead of 0? Remember that the index of a list begins counting at 0. If you are printing the current index as the number, your printing will start at 0. In order to start counting at 1, you will need to print something like index + 1. CMSC 201 Computer Science I for Majors Page 8

Part 3C: Voting for a Game This is the second of four steps that must be written for this lab. Now that the code to display the games is complete, we need to allow the user and their friends to actually vote! For now, we won t worry about storing the votes. Just write the code that will allow the user to vote, and that will stop when they enter a 0 to quit. If they make an invalid choice, simply ignore it and ask again. Here is some sample output, with the user input in blue. (Yours does not have to match this word for word, but it should be similar.) linux1[6]% python games.py 1 - Twister 2 - Dodgeball 3 - Capture the Flag 4 - Hide and Seek 5 - Croquet 6 - Ring Toss 7 - Ping Pong What is your favorite game? (Enter 0 to stop): 1 What is your favorite game? (Enter 0 to stop): 1 What is your favorite game? (Enter 0 to stop): 4 What is your favorite game? (Enter 0 to stop): 4 What is your favorite game? (Enter 0 to stop): 9 What is your favorite game? (Enter 0 to stop): 7 What is your favorite game? (Enter 0 to stop): 0 Once this part of the program works correctly, move on to the next step. Are you stuck on how to interact with the user? Take a look at the example on page 2 of a sentinel loop (an interactive while loop with a clear stop condition). You should use the same basic code setup to allow the user to keep voting until they choose to quit by entering 0. CMSC 201 Computer Science I for Majors Page 9

Part 3D: Storing Votes This is the third of four steps that must be written for this lab. Now that you can accept votes, we need to store them. We ll store the votes for the games in another, separate list of integers. For example, if the user voted for Twister once (choice 1), Ping Pong twice (choice 7) and Capture the Flag four times (choice 3), the vote list would look like: votes = 1 0 4 0 0 0 2 index 0 index 1 index 2 index 3 index 4 index 5 index 6 In other words, the votes stored at a given index in votes should be for the game stored at that same index in the games list. Remember, list indexing starts at 0, but we re presenting the choices to the user starting at 1, so the way you store votes will need to compensate for this offset. You ll also need to make sure you ignore invalid input don t try to count a vote for option #8, when there are only 7 choices! At the end, print out the list of votes, so you can ensure your program is working correctly. (Simply use print("votes:", votes) in your code.) Here is some sample output, with the user input in blue. We ve removed the list of games at the beginning to save space, but it should still be present in your output. (Yours does not have to match this word for word, but it should be similar.) linux1[5]% python games.py [[ games should be displayed here ]] What is your favorite game? (Enter 0 to stop): 2 What is your favorite game? (Enter 0 to stop): 7 What is your favorite game? (Enter 0 to stop): 7 What is your favorite game? (Enter 0 to stop): 5 What is your favorite game? (Enter 0 to stop): 0 [0, 1, 0, 0, 1, 0, 2] (If you need some help, hints are available after this sample output.) Once this part of the program works correctly, move on to the next step. CMSC 201 Computer Science I for Majors Page 10

Stuck on how to store the user s votes? You need a list of the same length as the number of games. It should be a list of integers, and since this is something we re using to count, they should all be initialized to zero. Still stuck on how to store the user s votes? Try creating a votes variable that contains exactly as many zeroes as the number of games. Something like this would work: votes = [0, 0, 0, 0, 0, 0, 0] Is your program counting the user s vote for the wrong games? Remember, the user s numbering starts at 1, but the indexing in a list starts at 0. If a user chooses to vote for game #3, the votes for that game are stored at votes[2], not votes[3]. Having trouble seeing the big picture of how your program should work? Try drawing a quick flowchart or planning out what needs to happen on paper in pseudocode. Don t worry about the specific details, just try to visualize what needs to happen overall. How do you stop once the user wants to quit? When do you need to ignore a user s vote? How are the votes stored? When do variables need to be initialized? CMSC 201 Computer Science I for Majors Page 11

Part 3E: Printing Out the Results This is the last of four steps that must be written for this lab. This last step is relatively simple, as you ve already done all of the hard work. For this step, we ll display the final votes for each game. (If your code that asks for and stores votes doesn t work correctly, you might also have to do some debugging. That s how programming works, sometimes!) Once the user has entered 0 in order to stop voting, you need to go through the list and print out the number of votes each game earned. You will need to iterate through both of the lists, in the same while loop, in order to print out each game and the number of votes it received. Also, remove the line of code that prints out the list of votes from the last step! IMPORTANT NOTE: You do not need to worry about figuring out which game won. Although this would be great practice to try out! Here is some sample output, with the user input in blue. We ve removed the list of game at the beginning to save space. (Yours does not have to match this word for word, but it should be similar.) linux1[5]% python games.py [[ games should be displayed here ]] What is your favorite game? (Enter 0 to stop): 2 What is your favorite game? (Enter 0 to stop): 7 What is your favorite game? (Enter 0 to stop): 2 What is your favorite game? (Enter 0 to stop): 5 What is your favorite game? (Enter 0 to stop): 4 What is your favorite game? (Enter 0 to stop): 18 What is your favorite game? (Enter 0 to stop): 0 Twister has 0 votes Dodgeball has 2 votes Capture the Flag has 0 votes Hide and Seek has 1 votes Croquet has 1 votes Ring Toss has 0 votes Ping Pong has 1 votes (If you need some help, hints are available after this sample output.) CMSC 201 Computer Science I for Majors Page 12

Are you stuck on how to print elements from two lists at the same time? Because we want to print two lists at once, we must use the same while loop to access their contents. Remember that both lists are the same length, and the indexes of games and votes match between the two lists. Still stuck on how to print two lists at once? You will want to use a loop similar to the one at the bottom of page 3. (The two lists in your program should be the same length, so it doesn t matter which one you use for the length.) CMSC 201 Computer Science I for Majors Page 13

Part 4: Completing Your Lab To test your program, make sure you ve enabled Python 3, then run the games.py file. Try a few different inputs to check that your program works. Submitting Since this lab is not an in-person lab, you will need to use the submit command to turn your completed lab in. Once your games.py file is complete, it is time to turn it in with the submit command, where the class is cs201, and the assignment is LAB6. Type in (all on one line) submit cs201 LAB6 games.py and press enter. linux1[4]% submit cs201 LAB6 games.py Submitting games.py...ok linux1[5]% If you don t get a confirmation like the one above, check that you have not made any typos or errors in the command. You can check that your lab assignment was submitted by following the directions in Homework 0. Double-check that you submitted your lab assignment correctly, since an empty file will result in a grade of zero for this assignment. CMSC 201 Computer Science I for Majors Page 14