CSCI 110, Spring 2011
Home | | Course Schedule | | Assignments | | Lecture NotesLab 4 solution
#Program: searchInventory.py #Author: Brenda Student #Class: CSCI 110 #Date: 2/22/11 #Assignment: Lab 4 #Purpose: Find the number of occurrences of an item in a list. inventory = ["gold coins", "axe", "healing potion", "axe", "shield", "lantern"] item = raw_input("What item would you like to search for? ") itemCount = 0 for x in inventory: if x == item: itemCount = itemCount + 1 if itemCount == 1: print "You have 1 " + item + "." else: print "You have", itemCount, item + "s."
#Program: addEvens.py #Author: Brenda Student #Class: CSCI 110 #Date: 2/22/11 #Assignment: Lab 4 #Purpose: Sum the even numbers from 2 to a high number. 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: " for x in range(2, highNum + 1, 2): print x total = total + x print "The total is:", total
#Program: timesTwo.py #Author: Brenda Student #Class: CSCI 110 #Date: 2/22/11 #Assignment: Lab 4 #Purpose: Multiply each item in a list by 2 theList = [1, 3, 17, 5, 6, 14] print "The values in the list are: " for i in theList: print i for x in range(len(theList)): theList[x] = theList[x] *2 print "The doubled list is: " for i in theList: print i