Assignment 3
Assignment 3
To be discussed in Tutorials:
Exercise 3-1 Tracing
a) Assign a value to x so that the character "A" is printed out:
x = _______________________
if x%2==0 and x%3==1:
print("A")
b) Assign values to x and y so that the character "D" is printed out. Hint: The values could be floating-
point numbers.
x = ____________________
y = ____________________
if not ((0<=x<=3) and (0<=y<=3)):
print("A")
elif y<=1 or y>=2:
print("B")
elif x<=1 or x>=2:
print("C")
else:
print("D")
It adds 30 minutes to the cooking time if the turkey has been stuffed.
weight = eval(input())
stuffed = input()
if weight < 14:
result = 195
if weight <= 20:
result = 240
else:
result = 270
if stuffed:
result = result + 30
print(result)
1
The program should work for any input. You are asked to check whether the program is correct or not.
If it is not correct, you should state why it is incorrect and re-write the program with the incorrect
statement(s) fixed.
Exercise 3-3
divisible
Write a Python program that given a number (product) from the user and checks if the number is divisible
by 7 and prints the multiples of that number. If the user enters a negative number, print "Try again."
Use conditional operators only.
Example 1:
Enter a number: 15
15 is not divisible by 7.
Example 2:
Enter a number: 35
This is the product of 7*5.
Example 3:
Enter a number: -2
Try again.
• If the first suitcase has a weight of 27Kg and the second Suitcase has a weight of
25Kg,then the program should display:
Suitcase 1 accepted
Suitcase 2 accepted
Total charge = 300 LE
• If the first suitcase has a weight of 45Kg and the second Suitcase has a weight of
20Kg, then the program should display:
Suitcase 1 rejected
Suitcase 2 accepted
Total charge = 0 LE
• If the first suitcase has a weight of 25Kg and the second Suitcase has a weight of
40Kg, then the program should display:
Suitcase 1 accepted
Suitcase 2 rejected
Total charge = 100 LE
• If the first suitcase has a weight of 25Kg and the second Suitcase has a weight of
18Kg, then the program should display:
Suitcase 1 accepted
Suitcase 2 accepted
Total charge = 100 LE
2
• What does the following program display for any boolean values x, y and z? Choose one answer
from the below choices and Justify.
x = eval(input())
y = eval(input())
z = eval(input())
if (x and y): #if (x == True and y == True)
print((not x) or z)
else:
print((x and y) or z)
a) The value of x
b) The value of y
c) The value of z
d) The value of x and y
e) Always true
f) True if either x and y are both True or z is True, and False otherwise.
• Consider a program which changes the value of a boolean variable that tracks whether a light is on
or off. Three people, creatively named Sarah, Ali, and Mina, who are claiming to be expert light
switchers have written different implementations of this function.
Sarah’s Version:
lightOn = eval(input())
if (lightOn):
lightOn = False
else:
lightOn = True
Ali’s Version:
lightOn = eval(input())
if (lightOn):
lightOn = False
if (not lightOn):
lightOn = True
Minas’s Version:
lightOn = eval(input())
lightOn = not lightOn
Are these implementations equivalent, i.e. performing the same task? Are all three people expert
light switchers? Why? Justify your answer.
To be solved in Labs:
3
Given the cost of the purchase, write an algorithm to calculate and print the money paid taking into
consideration the 10% sales taxes. The taxes are calculated on the amount after the discount.
• Input a dog’s age in dog years: 2 The dog’s age in human years is 21
• Input a dog’s age in dog years: 15 The dog’s age in human years is 73
• Input a dog’s age in dog years: -5 Age must be positive number.
Tank Capacity: 12
Gas Gauge Reading in percent: 50
Miles per Gallon: 30
Get Gas!
The algorithm should print out Get gas or Safe to proceed depending on if the car can cross the 200
miles with the gas remaining in the tank.
Extra Exercises:
4
The BMI is calculated using the weight divided by height squared, where weight is in kg and height is in
meters.
• A: 85-100
• B: 74-85
• C: 60-74
• D: 50-60
• F: <50
Keeping in mind that a student cannot score more than 105 marks, nor less than 0 marks.
Write an algorithm that reads each student’s marks, print either a grade or an error message.
grade = eval(input())
if(grade <= 5):
print("this student is in elementary school")
elif(grade <= 8):
print("this student is in middle school")
elif(grade <= 12):
print("this student is in high school")
a) Write an equivalent algorithm that will print the same messages as the algorithm above without
using any nested if-statements.
b) Discuss the drawback of your algorithm? Hint: Compare the efficiency of both algorithms.
5
balance = eval(input())
if((balance >= 2000) or (balance <= 3000)):
print("Your balance is between 2000$ and 3000$")
print("Your interest rate will be 3.5%")
else:
print("Your balance is larger than 3000$")
print("Your interest rate will be 4.5%")
a) Do you think this algorithm does what the programmer intended? Justify your answer.
b) If the algorithm does not do what the programmer intends, improve this algorithm.