CSCI 110, Spring 2011
Home | | Course Schedule | | Assignments | | Lecture NotesLaboratory 4
Problem 1:
You should use the following template for your program:
Due at the end of today's class
In this problem you will write the program code to do the work of the count( ) function in
Python. Write a program, named searchInventory.py, that asks the user what item they would like
to search for and then uses
a for loop to search for that item in a list. The program should print out the number of times the
item occurs in the list. If there are zero or more than one occurence of the item, the program
should add an 's' to the name of the item when it reports the number found. Note: Do not use the
count( ) function to solve this problem!
#Program: searchInventory.py
#Include your program prologue here
inventory = ["gold coins", "axe", "healing potion", "axe", "shield", "lantern"]
item = raw_input("What item would you like to search for? ")
itemCount = 0
#Use a for loop here to examine each item in the list and count the number of
#occurrences of item
if itemCount == 1:
print "You have 1 " + item + "."
else:
print "You have", itemCount, item + "s."
The following are sample outputs of the program:
Sample output 1:
What item would you like to search for? axe You have 2 axes.
Sample output 2:
What item would you like to search for? shield You have 1 shield.
Problem 2: Write a program, named addEvens.py, that uses a for loop to sum up all the even numbers from 2 to some high number entered by the user.
You may use the following program template:
#Program: addEvens.py #Include your program prologue here print "This program sums all the even numbers up to a given number." highNum = input("What is the highest number you would like to add? ") total = 0 print "The numbers are: " #Use a for loop with the range function to print and total all the even numbers #up to highNum print "The total is:", total
The following are sample outputs of the program:
Sample output 1:
This program sums all the even numbers up to a given number. What is the highest number you would like to add? 6 The numbers are: 2 4 6 The total is: 12
Sample output 2:
This program sums all the even numbers up to a given number. What is the highest number you would like to add? 9 The numbers are: 2 4 6 8 The total is: 20
Problem 3:
Write a program, named timesTwo.py, that uses a for loop to multiply the value of each number
in a list by 2. The program must change the values in the list and then print out the
updated values. You should use the following program template:
#Program: timesTwo.py #Include your program prologue here theList = [1, 3, 17, 5, 6, 14] print "The values in the list are: " for i in theList: print i #Use a for loop here to multiply each item in the list by two. print "The doubled list is: " for i in theList: print i
The following is the output of the program:
Sample output:
The values in the list are: 1 3 17 5 6 14 The doubled list is: 2 6 34 10 12 28
What To Turn In. A printed listing of the lab programs.
Be sure that your name is in the program prologues comment section of the program.
Reminder. Be sure to save a copy of
each program on your