ITC 106 Activities(review question programming excercises

Chapter 1
1.1
This is a test of Python interpreter.
>>> ================================ RESTART ================================
>>>
This is a test of IDLE.
>>>
1.2
print(‘This is a test of IDLE.’)

Chapter 2

2.1 Personal Information
name = input(‘Please Enter your name: ‘)
address = input(‘Please enter your address with city, state and zip code: ‘)
number = input(‘Please enter your number: ‘)
major = input(‘Please enter your college major: ‘)

print()
print(‘Your name is: ‘, name)
print(‘Your address is: ‘, address )
print(‘Your number is: ‘, number)
print(‘Your college major is: ‘, major)

2.2 Sales Prediction
def main():
sales = float(input(‘Please enter projected sales: $’))

projected_sales  = sales * (0.23/1.00)
print(‘The projected sales are:  $’, projected_sales)
main()

2.4 Total Purchase
def main():
item1 = int(input(‘Please enter price of item 1: $’))
item2 = int(input(‘Please enter price of item 2: $’))
item3 = int(input(‘Please enter price of item 3: $’))
item4 = int(input(‘Please enter price of item 4: $’))
item5 = int(input(‘Please enter price of item 5: $’))
print()
total = item1 + item2 + item3 + item4 + item5
tax = total *(0.06/1.00)
print(‘Your total sales are: ‘, total)
print(‘Your tax is: ‘, format(tax, ‘.2f’))

main()

Chapter 3
3.2 Sales Tax Program Refactoring
def main():
purchase = get_price()
purchase, state_tax, country_tax, total_tax, total = calculate(purchase)
display(purchase, state_tax, country_tax, total_tax, total)

def get_price():
purchase = int(input(‘Please enter the amount of purchase: $’))

return purchase

def calculate(purchase):
state_tax = purchase * (0.04/100)
country_tax = purchase * (0.02/100)
total_tax = state_tax + country_tax
total = purchase + state_tax + country_tax

return purchase, state_tax, country_tax, total_tax, total

def display(purchase, state_tax, country_tax, total_tax, total):
print()
print(‘The amount of purchase was: $’, purchase)
print()
print(‘The state tax is: $’, state_tax)
print()
print(‘The country tax is: $’, country_tax)
print()
print(‘The total tax is: $’, total_tax)
print()
print(‘The total amount is: $’, total)
print()

main()

3.5 Property Tax
def main():
value = get_input()
value, property_assesment, property_tax = calculate(value)
display(value, property_assesment, property_tax)

def get_input():
value = int(input(‘Please enter the value of your property: $’))

return value

def calculate(value):
property_assesment = value * (0.60/1.00)
property_tax = (property_assesment / 100) * 0.64
return value, property_assesment, property_tax

def display(value, property_assesment, property_tax):
print()
print(‘The value of the property is: $’, format(value, ‘.2f’))
print()
print(‘The assessment value of the property is: $’, format(property_assesment, ‘.2f’))
print()
print(‘The property tax is: $’, format(property_tax, ‘.2f’))

main()

3.6 Body Mass Index
def main():
height, weight = get_input()
calculateDisplay(height, weight)

def get_input():
height = float(input(‘Please enter your height in inches: ‘))
weight = float(input(‘Please enter your weight in pounds: ‘))
return height, weight

def calculateDisplay(height, weight):
bmi = weight * (703 / height ** 2)
print(‘Your BMI is’, format(bmi, ‘.2f’))
main()

Chapter 4
4.5 Colour Mixer
def main():
answer = input(‘Please enter two primary colours(red and blue, red and yellow or blue and yellow): ‘)

if answer == ‘red and blue’:
print()
print(‘Read and blue make Purple’)
elif answer == ‘red and yellow’:
print()
print(‘Red and yellow make Orange’)
elif answer == ‘blue and yellow’:
print()
print(‘Blue and yellow make Green’)
else:
print(‘Please enter either red, blue or yellow’)
print()
answer2 = input(‘Would you like to run the program again?(Y/N): ‘).upper()
if answer2 == ‘Y’:
print()
print(‘Restarting…’)
print()
main()
elif answer2 == ‘N’:
print()
print(‘Farewell!’)
main()

4.7 Book Club Points
def main():
books = get_input()
club_points(books)
restart()

def get_input():
books = float(input(‘Please enter the amount of books you have purchased this month: ‘))

return books
def club_points(books):
if books == 0:
print()
print(‘You have earned 0 points’)
elif books == 1:
print()
print(‘You have earned 5 points’)
elif books == 2:
print()
print(‘You have earned 15 points’)
elif books == 3:
print()
print(‘You have earned 30 points’)
elif books >= 4:
print()
print(‘You have earned 60 points’)

def restart():
print()
answer = input(‘Would you like to run program again?(Y/N): ‘).upper()
if answer == ‘Y’:
print()
print(‘Restarting….’)
main()
elif answer == ‘N’:
print()
print(‘Farewell!’)
else:
print(‘Please enter either Y, y or N, n’)
restart()

main()

4.10 Body Mass Enhancement
def main():
height, weight = get_input()
bmi = calculateDisplay(height, weight)
display(bmi)
restart()

def get_input():
height = float(input(‘Please enter your height in inches: ‘))
weight = float(input(‘Please enter your weight in pounds: ‘))
return height, weight

def calculateDisplay(height, weight):
bmi = weight * 703 / height ** 2
print(‘Your BMI is’, format(bmi, ‘.2f’))
return bmi
def display(bmi):
if bmi > 25:
print()
print(‘You are considered overweight, possibly try some excercises?’)
elif bmi < 18.5:
print()
print(‘You are at optimal weight’)
elif bmi > 18.5:
print()
print(‘You are underweight, eat something!’)
else:
print(‘Please try again’)
main()
def restart():
answer = input(‘Would you like to restart program? (Y/N): ‘).upper()

if answer == ‘Y’:
print()
print(‘Restarting’)
main()
elif answer == ‘N’:
print(‘Farewell!!!’)
else:
print(‘Please enter either Y or N’)
restart()
main()

Chapter 5
5.1 Bug Collector
def main():
get_input()

def get_input():
days = 7
total = 0.0
print(‘This program will collected the number of bugs collected in a 7 day week and calculate the running total’)
print()

for counter in range(days):
bugs = float(input(‘How many bugs have you collected? ‘))
total = total + bugs
print()
print(‘Days\Total’)
print(‘———-‘)
print(days, ‘\t’, total)

main()

5.2 Calories Burned
def main():
print()
print(‘Minutes\t\tCalories Burned’)
print(‘————————‘)

for i in(10, 15, 20, 25, 30):
burned = 3.9 * i
print(i, ‘\t’, ‘\t’, burned)

main()

5.3 Budget Analysis
def main():
total = 0.0
print()
print(‘This program will get the user to enter a budget, ask for weekly expenses and tell the user if he/she’+ \
‘is over there budget’)
print()

budget = float(input(‘Please enter your budget for this month: ‘))

for i in range(4):
expenses = float(input(‘Please enter your expenses for the week: ‘))
total = total + expenses
new_budget = budget – total

if total < budget:
print()
print(‘You are under this month’)
print(‘Your budget was:’, format(budget, ‘.2f’),’and your expenses were:’, format(total, ‘.2f’))
print(‘You have’ ,new_budget, ‘left’)
elif total > budget:
print()
print(‘Unfornuately you are over this month’)
print(‘Your budget was:’, format(budget, ‘.2f’),’ and your expenses were:’,format(total, ‘.2f’))
print(‘You are over by’, new_budget)

main()

Chapter 6
6.2 Math Quiz
def main():
math()

def math():
import random
operand1 = random.randint(1,999)
operand2 = random.randint(1, 999)
equation = operand1 + operand2

print(‘\t \t’,operand1)
print(‘\t+\t’, operand2)
print(‘\t\t===============’)
answer = float(input(‘\t\t: ‘))

if answer == equation:
print(‘Congratulations, you got that correct!’)
else:
print(‘Im sorry but that was incorrect, the correct answer was: ‘, equation)

answer2 = str(input(‘Would you like another?(Y/N): ‘)).upper()

if answer2 == ‘Y’:
print()
print(‘Goodluck!’)
print()
math()

elif answer2 == ‘N’:
print(‘Goodbye!’)

main()

6.3 Maximum of two values
def main():
numbers1,numbers2 = numbers()
larger_number = maximum(numbers1, numbers2)
display(larger_number)

def numbers():
print(‘This program will tell which number is greater than the other’)
print()
numbers1 = int(input(‘Please enter a number?: ‘))
print()
numbers2 = int(input(‘Please enter another number?: ‘))

return numbers1, numbers2

def maximum(numbers1, numbers2):
if numbers1 > numbers2:
larger_number = numbers1
elif numbers1 < numbers2:
large_number = numbers2

return larger_number

def display(larger_number):
print()
print(‘The larger number is: ‘, larger_number)

main()

6.4 Falling Distance
def main():
loop()

def loop():
g = 9.8
for i in range (10):
distance = i

d = distance * 0.5 *(g * falling_distance())**2

print(distance)

print(distance,’\t’, d)

def falling_distance():
t = float(input(‘Please enter the amount of seconds that the object has been falling?: ‘))

return t

main()

Chapter 7
7.6 Average of Numbers
”’ Item Counter
Assume that a file containing a series of integers is named numbers.txt and exsists on the computer’s disk. Write a program
that calculates the average of all the numbers stored on file
numbers.txt
1
2
3
4
5
6
7
8
9
10
”’

def main():
print()
print(‘This program will read numbers from numbers.txt and calcualte the average’)
print()
numbers = []

file = open(‘numbers.txt’, ‘r’)

for line in file:
number_amount = float(line)
numbers.append(number_amount)

total = 0.0

for  value in numbers:
total += value
average = total/len(numbers)

print(‘The average is: ‘, average)

main()

7.4 Item Coutner
names.txt
Steven Casteletto
Mark Rebetzke
Sarah Ball
Steph Sayers
Ryan Kiely

def main():
names = []
file = open(‘names.txt’, ‘r’)
read = file.readline()
read = read.rstrip(‘\n’)

for i in file:
names_1 = i
names.append(names_1)

print(names)

search = False

while search == False:
read = file.readline()
read = read.rstrip(‘\n’)
print(names)
break

main()

Chapter 8
8.1 Total Sales
def main():
days_list =[‘Day 1′,’Day 2′,’Day 3′,’Day 4′,’Day 5’]
sales_list = []

for sales in days_list:
salesprices = int(input(‘Please enters sales price for’ + sales + ‘:  ‘))
sales_list.append(salesprices)

total_sales = 0

for total in sales_list:
total_sales += total

print(‘The total sales for this week is: $’, total_sales)

main()

8.4 Number Analysis Program
def main():
number_list = get_input()
calculateDisplay(number_list)

def get_input():
number_list = []

for i in range(20):
number = int(input(‘Please enter a number: ‘))
number_list.append(number)

return number_list

def calculateDisplay(number_list):
number_list.sort()
print()
print(‘The lowest number entered is: ‘, min(number_list))
print()
print(‘The highest number entered is: ‘, max(number_list))

total_numbers = 0

for total in number_list:
total_numbers += total
print()
print(‘The sum of all numbers is: ‘, total_numbers)

average = total_numbers/len(number_list)

print(‘The average of these numbers is: ‘, average)
main()

Chapter 9
9.1 Intials
def main():
name = get_name()
organise(name)

def get_name():
name = str(input(‘Please enter your full name (First, middle and last name): ‘))
return name

def organise(name):

wordSplit = name.split()

initialList = []

for ch in wordSplit:
initialList.append(ch[0])

newstr = ”
for item in initialList:
newstr += str(item)

print(newstr)

main()

9.3 Date
def main():
date = get_input()
process(date)

def get_input():
date = input(‘Please enter a date(dd/mm/yyyy): ‘)
return date

def process(date):
date_list = date.split(‘/’)

if date_list[0] == ’01’:
print(‘January’)
elif date_list[0] == ’02’:
print(‘February’)
elif date_list[0] == ’03’:
print(‘March’)
elif date_list[0] == ’04’:
print(‘April’)
elif date_list[0] == ’05’:
print(‘May’)
elif date_list[0] == ’06’:
print(‘June’)
elif date_list[0] == ’07’:
print(‘July’)
elif date_list[0] == ’08’:
print(‘August’)
elif date_list[0] == ’09’:
print(‘September’)
elif date_list[0] == ’10’:
print(‘October’)
elif date_list[0] == ’11’:
print(‘November’)
elif date_list[0] == ’12’:
print(‘December’)
else:
process(date)

print(‘,’, date_list[1],’,’,date_list[2])

main()

Chapter 10
10.1
def main():
dictionary()

def dictionary():
courseroom = {‘CS101’ : ‘3004’ ‘ Haynes” 8.00 a.m.’, ‘CS102’ : ‘4501” Alvarando” 9.00a.m’, ‘CS103’ : ‘6755’ ‘ Rich” 10.00a.m’, ‘NT110’ : ‘1244’ ‘ Burke” 11.00a.m’,\
‘CM241’ : ‘1411” Lee” 1.00p.m’}

room_number = input(‘Please enter course number: ‘).upper()
print(courseroom[room_number])

main()

10.2
import random
def main():
states = dictionary()
quizz(states)

def dictionary():
states ={‘Alabama’:’Montgomery’,’Alaska’:’Juneau’, ‘Arizona’:’Phoenix’, ‘Arkhansas’:’Little Rock’,’California’:’Los Angeles’,’Colarado’:’Denver’,\
‘Connecteticut’:’Hartford’,’Delaware’:’Dover’,’Florida’:’Miami’,’Georgia’:’Atlanta’,’Hawaii’:’Honolul’, ‘Idaho’:’Boise’,’Illinois’:’Chicargo’,\
‘Indiana’:’Indianapolis’,’Iowa’:’Des Moines’,’Kansas’:’Topeka’,’Kentucky’:’Frankfort’,’Louisiana’:’Baton Rouge’,’Maine’:’Augusta’,’Maryland’:’Annapolis’,\
‘Michigan’:’Lansing’,’Massachusetts’:’Boston’,’Minnesota’:’Saint Paul’,’Mississippi’:’Jackson’,’Missouri’:’Jefferson City’,’Montana’:’Helena’,\
‘Nebraska’:’Lincoln’,’Nevada’:’Carson City’,’New Hampshire’:’Concord’,’New Jersey’:’Trenton’,’New Mexico’:’Santa Fe’,’New York’:’Albany’,\
‘North Carolina’:’Raleigh’,’North Dakota’:’Bismarck’,’Ohio’:’Columbus’,’Oklahoma’:’Oklahoma City’,’Oregon’:’Salem’,’Pennsylvania’:’Harrisburg’,\
‘Rhode Island’:’Providence’,’South Carolina’:’Columbia’,’South Dakota’:’Pierre’,’Tennessee’:’Nashville’,’Texas’:’Austin’,’Utah’:’Salt Lake City’, \
‘Vermont’:’Montpelier’,’Virgina’:’Richmond’,’Washington’:’Olympia’,’West Virgina’:’Charlestown’,’Wisconsin’:’Madison’, ‘Wyoming’:’Cheyenne’}
return states

def quizz(states):
wrong = []

print(‘Test! \n’)

incorrect = False

while len(states)>0:
pick = random.choice(list(states.keys()))
correct = states.get(pick)
print(‘What is the capital city of’, pick,’?’)
answer = input(‘Enter your answer: ‘)
if answer.lower()==correct.lower():
print(‘That is Correct!’)
else:
print(‘Wrong!’)

main()

Chapter 11
11.1 Pet Class
class Pet:
def __init__(self, animal_type):
self.animal = animal_type

def setName(self,set_name):
self.name = set_name

def setAge(self, age):
self.age = age

def getType(self):
return self.animal

def getName(self):
return self.name

def getAge(self):
return self.age

def main():
animal_1 = Pet(input(‘Enter the type of animal: ‘))
animal_1.setName(input(‘Enter name for the animal: ‘))
animal_1.setAge(input(‘Enter age for the animal: ‘))

print()
print(‘The animal is a’, animal_1.getType())
print()
print(‘The name of the animal is’, animal_1.getName())
print()
print(‘The age of the animal is’, animal_1.getAge())
main()

11.2
class Car:
def __init__(self):
self.name = input(‘Enter car make: ‘)
self.year = input(‘Enter car year: ‘)
self.make = input(‘Enter car model: ‘)
self.speed = 0.0

def accelerate(self):
self.speed = 5.0 + self.speed

def brake(self):
self.speed = self.speed – 5.0

def getSpeed(self):
return self.speed

def getDetails(self):
print(self.name)
print(self.year)
print(self.make)

def main():

car1 = Car()
print()
print(car1.getDetails())
print()
print(‘The starting speed is:’, car1.getSpeed())

for i in range(5):
car1.accelerate()
print(“Accelerating…”, car1.getSpeed())

print()
print(‘The current speed is:’, car1.getSpeed())

for i in range(5):
car1.brake()
print(“Braking…”, car1.getSpeed())

print()
print(‘The current speed is:’,car1.getSpeed())

main()

11.4
employes.txt
Employee
Name: Joy Rogers
ID number: 81774
Department: Manufacturing
Job Title: Enginerr
Employee
Name: Mark Jones
ID number: 39119
Department: IT
Job Title: Programmer
Employee
Name: Susan Meyers
ID number: 47899
Department: Accounting
Job Title: Vice President

class Employee:
def __init__(self, name):
self.name = name

def setID(self, IDnum):
self.IDnum = IDnum

def setDepartment(self, department):
self.department = department

def setJob(self, job):
self.job = job

def getName(self):
return self.name

def getID(self):
return self.IDnum

def getDepartment(self):
return self.department

def getJob(self):
return self.job

def main():

employee = Employee

employee_file = open(‘Employees.txt’, ‘r’)

line = employee_file.read()
print(line)

answer = input(‘Do you want to enter employees details?’)
print()
print(‘Please enter the details’)
print()

if answer == ‘yes’:
file = open(‘Employees.txt’, ‘a’)
employee1 = employee(input(‘Please enter employees name:’))
employee1.setID(input(‘Please enter employess ID number:’))
employee1.setDepartment(input(‘Please enter employee department:’))
employee1.setJob(input(‘Please enter the Job Title:’))

file.write(str(‘Employee’) + ‘\n’)
file.write(str(‘Name: ‘))
file.write(employee1.getName()+’\n’)
file.write(str(‘ID number: ‘))
file.write(employee1.getID()+’\n’)
file.write(str(‘Department: ‘))
file.write(employee1.getDepartment()+’\n’)
file.write(str(‘Job Title: ‘))
file.write(employee1.getJob()+’\n’)
file.write(”)

main()

elif answer != ‘yes’:
print()
print(‘Goodbye’)
import sys
#sys.exit()

employee_file = open(‘Employees.txt’, ‘r’)

line = employee_file.readline()
print(line)

main()