Lab 8: Lists
Program 1: Color counter
colors.py
'''
Author name: <Your name here>
This program will count colors in a list of colors.
'''
# Function that takes as input a string of colors separated
# by spaces called colorString` and a second input called colorToCount.
# The function will count the colorToCount in the string of colors.
def countColor(colorString, colorToCount):
# First split the string into a list.
colorList = colorString.split()
# Count the color
count = colorList.count(colorToCount)
# Return the count
return count
# Input the colors list and the color to count.
colors = input("Enter colors: ")
color_to_count = input("Enter color to count: ")
print(color_to_count, "appears", countColor(colors, color_to_count), "times")
Program 2: Find Names
names.py
'''
Author name: <Your name here>
This program will find the position of a name in a list of names.
'''
# Function takes as input a string of names separated by
# spaces and a second input argument called nameToFind.
# The function will return the first position of nameToFind in the string of
# names and return -1 if nameToFind doesn’t exist in the string of names.
def findName(namesString, nameToFind):
# Split the names string into a list.
namesList = namesString.split()
# If the name is in the name list.
if nameToFind in namesList:
# The get the index of the first time the name is found.
# We add one to this zero based (starts at zero) index to get
# the position in the list which is one based (starts at one).
return namesList.index(nameToFind) + 1
else:
return -1
# Input the names list and the name to find.
names = input("Enter names: ")
name_to_find = input("Enter a name to find: ")
position = findName(names, name_to_find)
if position > 0:
print(name_to_find, "exists at the position number", position)
else:
print(name_to_find, "not found in list")
Program 3: Scores
scores.py
'''
Author name: <Your name here>
Reads an unspecified number of scores and determines how many
scores are above or equal to the average and how many scores
are below the average.
'''
# Finds the average of a list of scores.
def findAverage(scores_list):
total = 0
# For each score in the list of scores add it to the total.
for score in scores_list:
total += score
# Return the average which is the total divided by the the length of the list.
return total / len(scores_list)
# Function to find the number of scores that are above or equal to the average
def aboveAndEqualToAverageCount(scores_list, average):
total = 0
# For each score in the scores list compare to the average.
for score in scores_list:
# If the score is above or equal to the average increment the total.
if score >= average:
total += 1
return total
# Function to find the number of scores that are below average
def belowAverageCount(scores_list, average):
total = 0
# For each score in the scores list compare to the average.
for score in scores_list:
# If the score is below average increment the total.
if score < average:
total += 1
return total
# Input the scores.
scores = input("Enter scores: ")
# Convert the scores string to a list by using the split function.
scores_list_of_strings = scores.split()
# Convert the scores list from a list of strings
# to a list of numbers
# Start with an empty list
scores_list = []
# For each score string in the scores list
for score_string in scores_list_of_strings:
# Append the score to the scores list
# after using the eval function to convert
# to the score string to a number.
scores_list.append(eval(score_string))
# Find the average score.
average = findAverage(scores_list)
print("The average score is", average)
print(aboveAndEqualToAverageCount(scores_list, average), "are above ane equal to the average")
print(belowAverageCount(scores_list, average), "scores are below the average")