0% found this document useful (0 votes)
17 views

Lab 3

Uploaded by

engmanalf98
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lab 3

Uploaded by

engmanalf98
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab 3 - Elementary Programming I

Chapter: 2. Elementary Programming Lab


Time: 80 Minutes 3
Objectives
 To familiarize students how to solve practical problems programmatically.
 To practice on elementary programming by using Python built-in data types, variables,
constants, operators, expressions, and input and output.

Current Lab Learning Outcomes (LLO)


By completion of the lab the students should be able to
 Use the input function.
 Use variables.
 Use constants.
 Use arithmetic operators.
 Distinguish between float division operator and integer division operator.
 Write simple calculations.

Lab Requirements
 PyCharm (IDE).

Version 1.2
05/10/2019
Practice Activities with Lab Instructor (20 minutes)

Problem 1 Programming Exercises (2.2)

Write a program that reads in the radius and length of a cylinder and computes the area
and volume using the following formulas:

Here is a sample run:

Enter the radius and length of a cylinder: 5.5, 12 <enter>


The area is 95.0330975
The volume is 1140.39717

Solution

Phase 1: Problem-Solving Phase:


1- Ask the user to enter the radius and length of a cylinder (radius, length).
o Use the input function to read inputs from the user.
o Use the eval function to evaluate the string input to numbers.
o Use the simultaneous assignment to assign values into many variables.
o radius, length = eval(input("message…"))
2- Calculate the length of a cylinder (area).
o area = radius * radius * 3.14159
3- Calculate the volume of a cylinder (volume).
o volume = area * length
4- Display the results (area, volume).
Phase 2: Implementation Phase:
1. Create a new project and name it “Lab 3”.
2. Create a new file and name it “activity_1.py”.
3. Write the following code in the file:

2
activity_1.py
1 # Enter radius of the cylinder
2 radius, length = eval(input("Enter the radius and length of a cylinder: "))
3
4 # Calculate the area of the cylinder
5 area = radius * radius * 3.14159
6 # Calculate the volume of the cylinder
7 volume = area * length
8
9 # Print the results
10 print("The area is", area)
11 print("The volume is", volume)

Problem 2 Programming Exercises (2.7)

Write a program that prompts the user to enter the minutes (e.g., 1 billion), and displays
the number of years and days for the minutes.
For simplicity, assume a year has 365 days. Here is a sample run:

Enter the number of minutes: 1000000000 <enter>


1000000000 minutes is approximately 1902 years and 214 days

Solution

Phase 1: Problem-Solving Phase:


1- Ask the user to enter the number of minutes (minutes).
o minutes = eval(input("message…"))
2- Calculate the number of days (numberOfDays).
o 1 Day = 24 Hours
o 1 Hour = 60 Minutes
o Use the integer division operator (//) to omit the floating-point from the result of the
division.
o numberOfDays = minutes // (24 * 60)
3- Calculate the number of years (numberOfYears).
o 1 Year = 365 Days
o numberOfYears = numberOfDays // 365
4- Calculate the remaining days (remainingDays).
o Get the remaining days that didn’t complete a year.
o remainingDays = numberOfDays % 365
5- Display the result (numberOfYears, remainingDays).
3
Phase 2: Implementation Phase:
1. Open the project “Lab 3” if it was not opened or create it if it was not existing.
2. Create a new file and name it “activity_2.py”.
3. Write the following code in the file:
activity_2.py
1 # Obtain input
2 minutes = eval(input("Enter the number of minutes: "))
3
4 # Calculate the number of days
5 numberOfDays = minutes // (24 * 60)
6 # Calculate the number of years
7 numberOfYears = numberOfDays // 365
8 # Calculate the remaining days
9 remainingDays = numberOfDays % 365
10
11 # Display results
12 print(minutes, "minutes is approximately",
13 numberOfYears, "years and", remainingDays, "days")

4
Individual Activities (60 minutes)

Problem 3 Programming Exercises (2.1)

Write a program that reads a Celsius degree from the console and converts it to
Fahrenheit and displays the result. The formula for the conversion is as follows:

Here is a sample run of the program:

Enter a degree in Celsius: 43 <enter>


43 Celsius is 109.4 Fahrenheit

Problem 4 Programming Exercises (2.8)

Write a program that calculates the energy needed to heat water from an initial
temperature to a final temperature. Your program should prompt the user to enter the
amount of water in kilograms and the initial and final temperatures of the water. The
formula to compute the energy is:

where M is the weight of water in kilograms, temperatures are in degrees Celsius, and
energy Q is measured in joules. Here is a sample run:

Enter the amount of water in kilograms: 55.5 <enter>


Enter the initial temperature: 3.5 <enter>
Enter the final temperature: 10.5 <enter>
The energy needed is 1625484.0

5
Extra Exercises (Homework)

From the Textbook


 Programming Exercises:
o 2.4
o 2.6
o 2.10

From MyProgrammingLab (https://pearson.turingscraft.com)


 2.5
o 51001
o 51014
o 51019
 2.6
o 51168
o 51169
o 51170
o 51171
o 51093

Upload Your Solutions


Upload your solutions of the lab activities to Blackboard.

You might also like