MONT 105S, Spring 2009
Home | | Course Schedule | | Assignments | | Lecture NotesLaboratory 5
Problem 1:
You should use the following template for your program:
The following are sample outputs of the program:
Sample output 2:
Problem 2:
Write a program 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:
The following are sample outputs of the program:
Sample output 2:
Problem 3:
The following is the output of the program:
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
Due at the end of today's class
Write a program 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.
#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."
Sample output 1:
What item would you like to search for? axe
You have 2 axes.
What item would you like to search for? shield
You have 1 shield.
#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
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
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
Write a program that uses a for loop to multiply the value of each number
in a list by 2. You may 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
Sample output:
The values in the list are:
1
3
17
5
6
14
The doubled list is:
2
6
34
10
12
28