My Python Lab Sheet3
My Python Lab Sheet3
LAB SHEET # 3
SUBMITTED TO
Title:
1. Bug Collector
A bug collector collects bugs every day for five days. Write a program that keeps a running
total of the number of bugs collected during the five days. The loop should ask for the
number of bugs collected for each day, and when the loop is finished, the program should
display the total number of bugs collected.
Algorithm:
a. Start
b. Initialize variables
c. Ask the user to enter the bugs
d. Using loop ask for the num of bugs collected each day
e. Display the total num of bugs collected
f. Stop
Flowchart:
Start
read bugs
loop
number = int(input())
print Total
Code:
days = 5
total = 0.0
print("Enter the num of bugs in 5 days")
for counter in range(days):
number = int(input())
Regn No.1442-320007 Muhammad Waqas Khalil MCS (Eve), Preston University Islamabad
[3]
Lab Sheet # 3 Programming Fundamentals
***END***
Regn No.1442-320007 Muhammad Waqas Khalil MCS (Eve), Preston University Islamabad
[4]
Lab Sheet # 3 Programming Fundamentals
Title:
2. Calories Burned
Running on a particular treadmill you burn 4.2 calories per minute. Write a program that
uses a loop to display the number of calories burned after 10, 15, 20, 25, and 30 minutes.
Algorithm:
a. Start
b. using loop check the calories
c. calculate the per minutes calories
d. Display the minutes and calories with interval of 5 minutes
e. Stop
Flowchart:
Start
num<30
per_minute=4.2*num
End
Code:
def main():
print('Minutes\tCalories')
for num in [10,15,20,25,30]:
per_minute=4.2*num
print(num,'\t',format(per_minute,'.2f'))
main()
Output (Compilation, Debugging & Testing):
Minutes Calories
10 42.00
15 63.00
20 84.00
25 105.00
30 126.00
Regn No.1442-320007 Muhammad Waqas Khalil MCS (Eve), Preston University Islamabad
[5]
Lab Sheet # 3 Programming Fundamentals
***END***
Title:
3. Lap Times
Write a program that asks the user to enter the number of times that they have run around
a racetrack, and then uses a loop to prompt them to enter the lap time for each of their
laps. When the loop finishes, the program should display the time of their fastest lap, the
time of their slowest lap, and their average lap time
Algorithm:
a. Start
b. Ask the user to enter the number of laps
c. Then ask the user to enter the time for all laps
d. Using if statement check fastest, slowest and average time lap
e. Display the result
f. Stop
Flowchart:
Regn No.1442-320007 Muhammad Waqas Khalil MCS (Eve), Preston University Islamabad
[6]
Lab Sheet # 3 Programming Fundamentals
Start
read laps
fastest
avg += lap_time
avg /= num_laps
End
Code:
num_laps = int(input('Enter number of laps you ran: '))
slowest, fastest, avg = 0, 1000, 0
for lap in range(num_laps):
lap_time = int(input('\nEnter the lap time in minutes for each lap from first to last: '))
if lap_time > slowest:
slowest = lap_time
if lap_time < fastest:
fastest = lap_time
avg += lap_time
avg /= num_laps
print('slowest = ', slowest)
print('fastest = ', fastest)
print('average = ', avg)
Output (Compilation, Debugging & Testing):
Enter number of laps you ran: 5
Enter the lap time in minutes for each lap from first to last: 15
Regn No.1442-320007 Muhammad Waqas Khalil MCS (Eve), Preston University Islamabad
[7]
Lab Sheet # 3 Programming Fundamentals
Enter the lap time in minutes for each lap from first to last: 12
Enter the lap time in minutes for each lap from first to last: 26
Enter the lap time in minutes for each lap from first to last: 17
Enter the lap time in minutes for each lap from first to last: 18
slowest = 26
fastest = 12
average = 17.6
***END***
Regn No.1442-320007 Muhammad Waqas Khalil MCS (Eve), Preston University Islamabad
[8]
Lab Sheet # 3 Programming Fundamentals
Title:
4. Distance Traveled
The distance a vehicle travels can be calculated as follows:
Distance = Speed x Time
For example, if a train travels 40 miles per hour for three hours, the distance traveled is 120
miles. Write a program that asks the user for the speed of a vehicle (in miles per hour) and
the number of hours it has traveled. It should then use a loop to display the distance the
vehicle has traveled for each hour of that time period. Here is an example of the desired
output:
What is the speed of the vehicle in mph? 40 Enter
How many hours has it traveled? 3 Enter
Hour Distance Traveled
1 40
2 80
3 120
Algorithm:
a. Start
b. Ask the user to enter the speed and time
c. Calculate the distance traveled
d. Using loop check the speed after time interval
e. Display the Time and Distance Traveled
f. Stop
Flowchart:
Start
t=<time
display speed must be greater than zero
dispaly distance
End
Regn No.1442-320007 Muhammad Waqas Khalil MCS (Eve), Preston University Islamabad
[9]
Lab Sheet # 3 Programming Fundamentals
Code:
speed = int(input('Enter mph: '))
time = int(input('Enter hours: '))
print('Hour\t:\tDistance Travelled')
if time <= 0 or speed <= 0:
print('Invalid Hours and mph must be greater than 0')
else:
for t in range(time):
distance = speed * (t+1)
print(t + 1,'\t:\t', distance)
Output (Compilation, Debugging & Testing):
Enter mph: 40
Enter hours: 3
Hour : Distance Travelled
1 : 40
2 : 80
3 : 120
***END***
Regn No.1442-320007 Muhammad Waqas Khalil MCS (Eve), Preston University Islamabad
[10]
Lab Sheet # 3 Programming Fundamentals
Title:
5. Average Rainfall
Write a program that uses nested loops to collect data and calculate the average rainfall
over a period of years. The program should first ask for the number of years. The outer loop
will iterate once for each year. The inner loop will iterate twelve times, once for each
month. Each iteration of the inner loop will ask the user for the inches of rainfall for that
month. After all iterations, the program should display the number of months, the total
inches of rainfall, and the average rainfall per month for the entire period.
Algorithm:
a. Start
b. Initialize the variables
c. Ask the user to enter the num of years
d. Ask the user to enter the monthly rain record
e. Calculate the total rain fall in year and average rain fall
f. Display the result
g. Stop
Flowchart:
Start
totalRainfall = 0.0
monthRainfall = 0.0
averageRainfall = 0.0
years = 0
totalMonths = 0
read years
totalMonths += 1
totalRainfall += monthRainfall
End
Regn No.1442-320007 Muhammad Waqas Khalil MCS (Eve), Preston University Islamabad
[11]
Lab Sheet # 3 Programming Fundamentals
Code:
totalRainfall = 0.0
monthRainfall = 0.0
averageRainfall = 0.0
years = 0
totalMonths = 0
years = int(input('Enter the number of years: '))
for year in range(years):
print ('For year ', year + 1, ':')
for month in range(12):
monthRainfall = float(input('Enter the rainfall amount for the 12 month: '))
totalMonths += 1
totalRainfall += monthRainfall
averageRainfall = totalRainfall / totalMonths
print('Total Month in Rainfall are ', totalMonths)
print('Total rainfall: ', totalRainfall,'inches')
print('Average monthly rainfall: ',averageRainfall,'inches')
Output (Compilation, Debugging & Testing):
Enter the number of years: 1
For year 1 :
Enter the rainfall amount for the 12 month: 12
Enter the rainfall amount for the 12 month: 45
Enter the rainfall amount for the 12 month: 56
Enter the rainfall amount for the 12 month: 89
Enter the rainfall amount for the 12 month: 78
Enter the rainfall amount for the 12 month: 78
Enter the rainfall amount for the 12 month: 12
Enter the rainfall amount for the 12 month: 23
Enter the rainfall amount for the 12 month: 56
Enter the rainfall amount for the 12 month: 89
Enter the rainfall amount for the 12 month: 45
Enter the rainfall amount for the 12 month: 23
Total Month in Rainfall are 12
Total rainfall: 606.0 inches
Average monthly rainfall: 50.5 inches
***END***
Regn No.1442-320007 Muhammad Waqas Khalil MCS (Eve), Preston University Islamabad