0% found this document useful (0 votes)
12 views7 pages

Abdullah Haris 11-D Q01c

The document contains a series of programming exercises involving Python code snippets. It includes examples of loops, conditionals, functions, and data structures to solve various problems such as calculating bonuses, validating months, determining leap years, sorting scores, and analyzing truck distances. Each section is labeled with a question number and demonstrates different programming concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

Abdullah Haris 11-D Q01c

The document contains a series of programming exercises involving Python code snippets. It includes examples of loops, conditionals, functions, and data structures to solve various problems such as calculating bonuses, validating months, determining leap years, sorting scores, and analyzing truck distances. Each section is labeled with a question number and demonstrates different programming concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Abdullah Haris 11-D

Q01c
# Q01c

count = 0

constantValue = 7

maxValue = 5

while (count < maxValue):

print(count, count + constantValue)

count = count + 1

Q01f
# Q01f

list_1f =[(2010,8), (4800, 11), (2011,4), (5000,9)]

assistantSalary = 1000.00

for pair in list_1f:

shopIncome = pair[0]

assistantSales = pair[1]

print ("Shop income :", shopIncome, "Assistant sales ", assistantSales)

if ( shopIncome > 5000 or assistantSales >= 10 ):

print ("Assistant bonus = ", assistantSalary * 0.1)

elif ( shopIncome >= 2000 or assistantSales >= 5 ):


print("Assistant bonus = ", assistantSalary * 0.05)

else:

print("Assistant bonus = ", 0)

Q02a
import random

#initialise variables

counter = 1

answer = random.randint(1,10)

guess = 0

#Print Prompt and take guess from user

guess = int(input("Enter a number from 1 to 10: "))

#Create while loop to check guess

while guess != answer:

counter += 1

if guess > answer:

print(guess, "was too high. Try again")

else:

print(guess, "was too low. Try again")

guess = int(input("Guess again: "))

#Report the correct answer to the user and display the number of guesses

print("You guessed", guess, "in", counter, "guesses")

Q03a
# Q03a
# Subprogram to get and validate the month number-----------------------------------------------------

def display():

monthNumber = 0

while monthNumber < 1 or monthNumber > 12:

monthNumber = int(input("Enter the month number [1 to 12]: "))

return monthNumber

# End of subprogram display --------------------------------------------------------------------------

# Subprogram to show the name of the month and the number of days in the month
-----------------------

def showMonthNameAndDays(pMonth):

# Month number, days in the month and month name

monthAndDays = [

[1, 31, "January"],

[2, 28, "February"],

[3, 31, "March"],

[4, 30, "April"],

[5, 31, "May"],

[6, 30, "June"],

[7, 31, "July"],

[8, 31, "August"],

[9, 30, "September"],

[10, 31, "October"],

[11, 30, "November"],

[12, 31, "December"],]

count = 0

found = False
while count < len(monthAndDays) and found == False:

if monthAndDays[count][0] == pMonth:

found = True

print(monthAndDays[count][2], "has", monthAndDays[count][1], "days")

count = count + 1

# End of subprogram showMonthNameAndDays ------------------------------------------------------------

# Main program ---------------------------------------------------------------------------------------

monthInput = display()

showMonthNameAndDays(monthInput)

Q03c
# Q03c

# Input requested

year = int(input("Enter a year in this format YYYY e.g. 2000: "))

# Process input and display the result

# Add your code here

if year % 400 == 0 or ( year % 4 == 0 and year % 100 != 0 ):

print(year, "is a leap year")

else:

print(year, "is not a leap year")

Q4
scores = [12, 5, 19, 3, 8, 15, 7]

#write your code here


# Creating a function

def bubble_sort(array):

for pass_ in range(len(array)):

for i in range(len(array) - pass_ - 1):

if array[i] > array[i+1]:

array[i], array[i+1] = array[i+1], array[i]

return array

# Getting the sorted values

new_scores = bubble_sort(scores)

# Outputing the result

print(new_scores)

Q5
# Athlete's performance data

running_time = float(input("Enter athlete's running time (in minutes): "))

push_ups = int(input("Enter number of push-ups: "))

# Evaluate performance using if-else statements

if ( running_time > 60 and push_ups < 30):

print("Athlete needs improvement in both categories")

elif ( running_time > 60 or push_ups < 30):

print("Athlete needs improvement in one category")

elif ( running_time < 40 and push_ups > 50):

print("Athlete performed excellently")

else:

print("Athlete performed well")


Q6
# Q6

# Structure of truck record is

# TruckID, Driver name, Day 1 distance, Day 2 distance, Day 3 distance,

# Day 4 distance, Day 5 distance, Day 6 distance, Day 7 distance

truckDistances = [

["T101", "John", "Doe", 120, 150, 180, 200, 90, 140, 160],

["T102", "Sarah", "Smith", 130, 145, 160, 170, 100, 135, 155],

["T103", "Michael", "Johnson", 110, 120, 140, 150, 80, 125, 130],

["T104", "Emily", "Davis", 140, 160, 175, 190, 95, 145, 165],

["T105", "Robert", "Brown", 125, 135, 155, 165, 85, 130, 150]

#--------------------------------------------------------------------------

# Write your code below this line

# calculating and outputing total distance for each truck

distance = []

for i in truckDistances:

distance.append(sum(i[3:]))

print(i[0],"covered", sum(i[3:]))

# outputing the total distance

print("\nThe total distance covered by the entire fleet is", sum(distance))

# adding index to with each distance

for x in range(len(distance)):

distance[x] = [x, distance[x]]


# sorting the distance array to find the highest

for pass_ in range(len(distance)):

for i in range(len(distance) - pass_ - 1):

if distance[i][1] < distance[i+1][1]:

distance[i], distance[i+1] = distance[i+1], distance[i]

# outputing the the values of the two trucks with the highest distance

for x in range(2):

num = distance[x][0]

print("\nTruck Id:",truckDistances[num][0],

"\nDriver name:",truckDistances[num][1],truckDistances[num][2],

"\nhighest total distance:", distance[x][1])

You might also like