Lab 1: Input, Processing, and Output

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Starting Out with Programming Logic and Design 1

Lab 1: Input, Processing, and Output


This lab accompanies Chapter 2 of Starting Out with Programming Logic & Design.
Modified by M. L. Malone

Name: ____Tijuann Scott______________________________

Lab 1.4 – Python Code

Critical Review
Comments in Python are preceded by the # sign

Input of strings into a variable is done using the raw_input function. This function converts the input to a
series of characters so they can be used later in the program. This is often written as an equation, such as:
stringVariable = raw_input(‘Enter a word.’)

Input of numeric values into a variable is done using the input function. The method of input is similar to
string input. For example:
realVariable = input(“Enter a decimal value.’)

Equations are written similarly to the method done in pseudocode without the Set keyword. For example,
total = apples + oranges

Complex formulas should use parentheses to group processes. In addition, if input values are taken in as
integers but will be used to calculate a decimal value, they must be converted to real values. For example:
average = (test1 + test2) / 2

To display information to the screen, the print command is used with the string to be displayed written within
single quotation marks. If you need to display the value of a variable after the string, separate the two by a
comma. For example:
print ‘The average is’, average

This lab requires you to translate your work in the pseudocode and flowchart to actual code using Python. Read
the following problem prior to completing the lab.

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.

Step 1: Examine the following line of code. What do you expect as output to the screen?

studentName = raw_input (‘Enter student name. ‘)

Enter student name.

Step 2: Examine the following line of code. What type of value do you expect the user to enter?
Starting Out with Programming Logic and Design 2

creditsDegree = input (‘Enter credits required for degree.‘)

The user will use a numeric value.

Step 3: Select (mark with an X) which function should be used to take in input from the user. The functions
raw_input or input are determined based on the data type of the variable?

raw_input() input()
studentName X
creditsDegree X
creditsLeft X

Step 4: If the user of the program types Bill Jones o the question in Step 1, what do you expect the output to
the screen to be when the following line of code process?

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.’

This program requires 63 credits and they have taken 43 credits so far.

Step 6: Start eh IDLE Environment for Python. If the Edit window for entering code does not come up, go to
Options, configure IDLE, click on the General tab, and under Startup Preferences select Open Edit Window.
Close and reopen the Environment. Prior to entering code, save your file by clicking on File and then Save.
Select our location and save this file as Lab 1-4.py. Be sure to include the .py extension.
Starting Out with Programming Logic and Design 3

Lab 1.5 – Programming Challenge 1: Team Average

Write the Algorithm (i.e. step stated in English, one per line), Pseudo code, and Flowchart for the following
programming problem.

Team Average

A college wants you to write a program for them that will calculate the average number of wins for the
football team over the past five years. The program user should be able to enter the number of wins
each year. The program will calculate the average 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.

The Algorithm
1. Input the total number of games for Year 1
2. Input the total games won for Year 1
3. Calculate the average for Year 1.
4. Repeat Steps 1-3 for years 2-5.
5. Calculate the average games won over the 5 year period.
6. Display the average games won over the 5 year period.

The Pseudo code

//Declare Variables
Declare Real totalGamesYear1
Declare Real totalGamesYear2
Declare Real totalGamesYear3
Declare Real totalGamesYear4
Declare Real totalGamesYear5
Declare Real gamesWonYear1
Declare Real gamesWonYear2
Declare Real gamesWonYear3
Declare Real gamesWonYear4
Declare Real gamesWonYear5
Declare Real avgYear1
Declare Real avgYear 2
Declare Real avgYear 3
Declare Real avgYear 4
Declare Real avgYear 5
Declare Real totalAverage

Set avgYear1 = totalGamesYear1 / gamesWonYear1


Set avgYear2 = totalGamesYear2 / gamesWonYear2
Set avgYear3 = totalGamesYear3 / gamesWonYear3
Set avgYear4 = totalGamesYear4 / gamesWonYear4
Set avgYear5 = totalGamesYear5 / gamesWonYear5
Set totalAverage = (avgyear1 + avgyear2 + avgyear3 + avgyear4 + avgyear5) / 5

Display “What is the total number of games played year1?”


Starting Out with Programming Logic and Design 4

Input totalGamesYear1
Display “How many games won year 1?”
Input gamesWonYear1
Calculate avgYear1
Display “What is the total number of games played year2?”
Input totalGamesYear2
Display “How many games won year 2?”
Input gamesWonYear2
Calculate avgYear2
Display “What is the total number of games played year3?”
Input totalGamesYear3
Display “How many games won year 3?”
Input gamesWonYear3
Calculate avgYear3
Display “What is the total number of games played year4?”
Input totalGamesYear4
Display “How many games won year 4?”
Input gamesWonYear4
Calculate avgYear4
Display “What is the total number of games played year5?”
Input totalGamesYear5
Display “How many games won year 5?”
Input gamesWonYear5
Calculate avgYear5
Calculate totalAverage
Display “The average number of games won over 5 years is:”, totalAverag

The Flowchart
Starting Out with Programming Logic and Design 5

Tijuann Scott
IT104 – Intro to Programming
April 4, 2011

Lab 1.6 – Programming Challenge – Pedometer Calculator

Write the Algorithm (ie step stated in English, one per line), Pseudo code, and Flowchart for the following
programming problem.

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.

The Algorithm
7. Input the day of the week on which the day is done.
8. Input the number of steps taken
9. Calculate the number of miles walked based on the number of steps taken.
10. Calculate the number of calories burned based on the number of miles walked.
11. Display the day of the week.
12. Display number of miles walked
13. Display number of calories burned.

The Pseudocode

//Declare Variables
Declare Real numberSteps
Declare Real calories

Set miles = numberSteps / 2000


Set calories = miles * 65

Display “What Is The Day of the Week?”


Input dayOfWeek
Display “How many steps did you walk?”
Input numberSteps
Calculate miles
Calculate calories
Display “On”, dayOfWeek, “you walked”, miles, “and burned”, calories, “calories.”

The Flowchart

You might also like