Download Solutions for python programming worksheets and more Study notes Computer Science in PDF only on Docsity!
Solutions
Lab 1: Algorithm, Pseudocode, Flowchart, and Python Basics
Lab 1.1 through 1.4 uses the following programming problem.
Write a program that will take in basic information from a student, including student name, degree name, number of credits taken so far, and the total number of credits required in the degree program. The program will then calculate how many credits are needed to graduate. Display should include the student name, the degree name, and credits left to graduate.
Lab 1.5 and 1.6 are Programming Challenge Problems.
Lab 1.1 – Algorithms
Step 1: Examine the following algorithm.
1. Get the student name.
2. Get the degree program name.
3. Subtract the number of credits taken so far from the required credits for the
degree.
4. Get the number of credits required for the degree program.
5. Get the number of credits the student has taken so far.
6. Display the input information in Step 1 and 2.
7. Display the calculated information.
Step 2: What logic error do you spot and how would you fix it?
Step 3 cannot be processed until Step 4 and 5 are processed.
1. Get the student name.
2. Get the degree name.
3. Get the number of credits required for the degree program.
4. Get the number of credits the student has taken so far.
5. Subtract the number of credits taken for the required credits for the degree.
6. Display the input information in Step 1 and 2.
7. Display the calculated information in Step 5.
Step 3 : What steps require user interaction (Ex: user must type in some input)?
Step 1, Step 2, Step 3, and Step 4
Lab 1.2 – Pseudocode
Step 1 : This program is most easily solved using just five variables. Identify potential
problems with the following variables declared in the pseudocode. Assume that the
college has the ability to offer half credits.
Variable Name Problem
(Yes or No)
If Yes, what’s wrong?
Declare Real creditsTaken No
Declare Real credits Degree Yes No spaces are allowed in variable
name
Declare Int creditsLeft Yes Wrong data type. Since this is used
in a formula that uses Reals, it too
should be a real. It is also possible a
college would grant a half credit.
Declare Real studentName Yes Wrong data type. Should be String.
Declare String degreeName No
Step 2: Complete the pseudocode by writing the three missing lines.
Display "Enter student name." Input studentName Display "Enter degree program." Input degreeName Display "Enter credits required for the degree." Input creditsDegree Display "Enter the number of credits taken so far." Input creditsTaken
Step 3 : What two things are wrong with the following calculation?
creditsLeft = creditsTaken – creditsDegree
There is no Set keyword used prior to the equation.
Expression is backwards, should be Set creditsLeft =
creditsDegree - creditsTaken
Step 4: Write the exact output you would expect from the following line of code if the
user of the program enters "Bill Jones".
Display "The student's name is ", studentName
The student’s name is Bill Jones
Lab 1.3 – Flowcharts
Lab 1-4: Python Code
Step 1: Examine the following line of code. What do you expect as output to the
screen?
studentName = input('Enter student name. ')
Enter student name.
Step 2: Examine the following line of code. What type of value do you expect the user
of the program to enter?
creditsDegree = int(input('Enter credits required for degree. '))
A number such as 63.
Step 4: If the user of the program types Bill Jones to the question in Step 1, what do
you expect the output to the screen to be when the following line of code processes?
print('The student's name is', studentName)
The student’s name is Bill Jones
Step 5: Examine the following line of code. If the program requires 63 credits, and the
student has 20 left, what do you expect the output to the screen to be?
print('The program requires', creditsDegree, 'credits and they have taken', creditsTaken, 'credits so far.')
The program requires 63 credits and they have taken 43 credits so far.
Step 1 7 : Complete Python code.
Python 3.8.
import sys
print(sys.version)
This program takes in student information and calculates
the number of credits the student has left before graduation.
Information is then printed to the screen.
Lab 1.5 – Programming Challenge 1 – Team Average
Team Average
A college wants you to write a program for them that will calculate the average
number of wins for their football team over the past five years. The user of the
program should be able to enter the number of wins each year. The program will
calculate the average number of wins during that five year period and display that
information to the screen.
Algorithm
1. Get the number of wins for year 1.
2. Get the number of wins for year 2.
3. Get the number of wins for year 3.
4. Get the number of wins for year 4.
5. Get the number of wins for year 5.
6. Add up wins for all years and divide by total number of years.
7. Display the calculated information in Step 6.
Pseudocode
- // This program calculates the average number of wins over the past
- // five years for a football team.
- // Declare variables
- Declare Real year
- Declare Real year
- Declare Real year
- Declare Real year
- Declare Real year
- Declare Real yearlyAverage
- // Ask for user input
- Display "Enter number of wins for year 1. "
- Input year
- Display "Enter number of wins for year 2. "
- Input year
- Display "Enter number of wins for year 3. "
- Input year
- Display "Enter number of wins for year 4. "
- Input year
- Display "Enter number of wins for year 5. "
- Input year
- // Calculates the average
- Set yearlyAverage = (year1 + year2 + year3 + year4 + year5) / 5
- // Display requested information
- Display "The average number of wins each year over",
- "the last five years is ", yearlyAverage
Flowchart
Start Stop Declare Real year Declare Real year Declare Real year Declare Real year Declare Real year Declare Real yearlyAverage Display “Enter number of wins for year 1.” Input year Display “Enter number of wins for year 2.” Input year Display “Enter number of wins for year 3.” Input year Display “Enter number of wins for year 5” Input year Set yearlyAverage = (year1 + year2 + year3 + year4 + year5) / 5 Display “The average number of wins each year over the last five years is”, yearlyAverage A A Display “Enter number of wins for year 4.” Input year
Lab 1.6 – Programming Challenge 2 – Pedometer Calculator
Pedometer Calculator
A dietician wants you to write a program that will calculate the number of calories
a person can lose by walking at a slow pace for a mile; however, the user will
have only the distance given by a pedometer, which is measured in steps and not
miles. Assume each mile a person walks is equivalent to 2000 steps, and that for
every mile walked, a person loses 65 calories. Allow the user of the program to
enter the number of steps taken throughout the day. The program will calculate
the distance in miles and the number of calories lost. The user of the program
should also be able to enter the day of the week the data is being calculated for.
The day of the week, the distance in miles, and the calories lost should then be
displayed to the screen.
Algorithm
1. Get the day of the week.
2. Get the number of steps reported on the pedometer.
3. Divide the number of steps taken by 2000, which is the number of steps per mile.
4. Multiply the result of Step 3 times 65, which is the number of calories burned per
mile.
5. Display the input information in Step 1.
6. Display the calculated information in Step 4 and 5.
Pseudocode
- // This program will calculate how many calories are burned
- // based on the number of steps taken throughout a day.
- // This is based on 2000 steps equals 1 mile and
- // 1 mile of walking burns 65 calories.
- // Declare variables
- Declare String weekDay
- Declare Integer stepsTaken
- Declare Integer caloriesLost
- Declare Real milesWalked
- // Ask for user input
- Display "Enter the day of the week. "
- Input weekDay
- Display "Enter the number of steps reported on the pedometer. "
- Input stepsTaken
- // The calculations
- Set milesWalked = stepsTaken / 2000
- Set caloriesLost = milesWalked * 65
- // Display the output
- Display "The following is data for, " weekDay
- Display "Walking ", milesWalked, " miles results in ",
- caloriesLost, " calories lost."
Flowchart
Start Stop Declare String weekDay Declare Integer stepsTaken Declare Integer caloriesLost Declare Real milesWalked Display “Enter the day of the week.” Input weekDay Display “Enter the number of steps reported on the pedometer.” Input stepsTaken Set milesWalked = stepsTaken / 2000 Set caloriesLost = milesWalked * 65 Display “The following is data for “, weekDay Display “Walking “, milesWalked, “ miles results in “, caloriesLost, “ calories burned.”
Solutions
Lab 2 : Decisions and Boolean Logic
Lab 2 .1 walks the student through the process of evaluating Boolean expressions.
Labs 2 .2, 2 .3, and 2 .4 use the following programming problem.
A retail company assigns a $5000 store bonus if monthly sales are $100,000 or more. Additionally, if their sales exceed 125% or more of their monthly goal of $90,000, then all employees will receive a message stating that they will get a day off.
Lab 2 .5 uses the following programming problem.
Write a program that will ask the user to enter a person's age, their weight, and their birth month. Your program will compare the entered values to the following and print the appropriate responses. Be sure to use modules. The Secret Answers The Comparison The Printed Response age = (^25) If the guessed age is less than or equal to 25 Congratulations, the age is 25 or less weight = (^128) If the guessed weight is greater than or equal to 128 Congratulations, the weight is 128 or more birthMonth = 'April' (^) If the guessed birth month is equal to April Congratulations, the birth month is April
Lab 2 .1 – Evaluating Conditions
Step 1: Consider the following values set to variables.
• myAge = 32
• yourAge = 18
• myNumber = 81
• yourNumber = 17
• votingAge = 18
• myName = "Katie"
• yourName = "Bob"
Step 2: Based on the values to the variables in Step 1, do the following conditions result
in a true or false statement?
The condition True or False
myAge >= yourAge True
yourAge > myAge False
myAge == 45 False
yourAge == votingAge True
votingAge >= yourAge True
myAge <= votingAge False
myName != yourName True
myNumber <= myAge False
yourNumber >= myAge False
yourNumber != 17 False
Step 3: Based on the values to the variables in Step 1, what is the expected output?
Hint: The output will either be what is printed to the screen, or "nothing."
The condition Expected Output
If myName == yourName Then print "We have the same name" End If
Nothing
If myAge >= yourAge Then print "I am older or equal to your age" End If
I am older or equal to
your age
If myName != "Katie" Then print "That is not my name" End If
Nothing
If myName == "Katie" Then print "That is my name" End If
That is my name
If myNumber == 17 Then print "My number is 17 " End If
Nothing
If myNumber >=80 Then print "My number is 80 or more" End If
My number is 80 or more
If yourNumber <= yourAge Then print "Your number is less than or equal to your age" End If
Your number is less than
or equal to your age
If myNumber < yourNumber Then print "My number is less" End If
Nothing
If yourAge >= votingAge Then print "You can vote" End If
You can vote
If myAge < yourAge Then
print "I am younger" End If
Nothing
Lab 2 .3 – Flowcharts
Lab 2 .4 – Python Code
Python 3.8.
import sys
print(sys.version)
Student Name
Date
Description: This program determines if a bonus should be
awarded
print('Welcome to the program')
gets the monthly sales
monthlySales = float(input('Enter the monthly sales $'))
determines if there is a bonus
if monthlySales >= 100000: print("You have earned a $5,000 bonus!!!")
determines if employees get a day off
if monthlySales >= 112500: print("All employees get a day off!!!")
The Flowchart
The Python Code
Python 3.8.
import sys
print(sys.version)
input the age
age = int(input('Enter your guess for age: '))
input the weight
weight = int(input('Enter your guess for weight: '))
input the age
birthMonth = input('Enter your guess for birth month: ') print() # prints a blank line
determine if the values entered are correct
if age <= 25: print('Congratulations, the age is 25 or less.') if weight >= 128: print('Congratulations, the weight is 128 or more.') if birthMonth == 'April': print('Congratulations, the birth month is April.')