3.7 Popcorn and Homework Hacks
Mihir's Submission for Hacks from Lesson
Homework Hacks
Write pseudocode to determine if a student passes a class based on their exam scores and attendance using nested conditionals.
Write a python segment to decide the shipping cost based on the weight of a package and the delivery speed chosen (standard or express) using nested conditionals.
Write a python segment to have different ticket prices for different ages, with a discount for students
1. PSEUDO CODE
START
INPUT exam_score
INPUT attendance_rate
IF exam_score >= 50 THEN
IF attendance_rate >= 75 THEN
PRINT "Student passes the class."
ELSE
PRINT "Student fails due to low attendance."
END IF
ELSE
PRINT "Student fails due to low exam score."
END IF
END
2. Python code to decide the shipping cost based on the weight of a package and the delivery speed using nested conditionals
# Input the weight of the package and delivery speed
weight = float(input("Enter the weight of the package (in kg): "))
delivery_speed = input("Enter the delivery speed (standard/express): ").lower()
# Determine the shipping cost using nested conditionals
if delivery_speed == "standard":
if weight <= 1:
cost = 5.00
elif weight <= 5:
cost = 10.00
else:
cost = 15.00
elif delivery_speed == "express":
if weight <= 1:
cost = 10.00
elif weight <= 5:
cost = 20.00
else:
cost = 30.00
else:
cost = 0
print("Invalid delivery speed.")
# Output the shipping cost
if cost > 0:
print(f"The shipping cost is ${cost}.")
The shipping cost is $30.0.
3. Python code to have different ticket prices for different ages, with a discount for students
# Input the age and student status
age = int(input("Enter your age: "))
student_status = input("Are you a student? (yes/no): ").lower()
# Determine the ticket price using nested conditionals
if age < 12:
ticket_price = 5.00
elif age >= 12 and age <= 18:
ticket_price = 10.00
elif age > 18 and age < 60:
ticket_price = 15.00
else:
ticket_price = 7.00
# Apply student discount if applicable
if student_status == "yes":
ticket_price *= 0.80 # 20% discount for students
# Output the final ticket price
print(f"The ticket price is ${ticket_price:.2f}.")
The ticket price is $8.00.
CHALLENGE Hack
# Function to check if the sides form a valid triangle
def is_valid_triangle(a, b, c):
return a + b > c and b + c > a and c + a > b
# Get the lengths of the triangle sides from the user
a = float(input("Enter the length of side 1: "))
b = float(input("Enter the length of side 2: "))
c = float(input("Enter the length of side 3: "))
# Check if inputs are positive numbers
if a <= 0 or b <= 0 or c <= 0:
print("Error: All sides must be positive numbers.")
else:
# Check if the sides form a valid triangle
if is_valid_triangle(a, b, c):
# Determine the type of triangle
if a == b == c:
print("This is an Equilateral Triangle (All sides are equal).")
elif a == b or b == c or c == a:
print("This is an Isosceles Triangle (Two sides are equal).")
else:
print("This is a Scalene Triangle (No sides are equal).")
else:
print("The sides do not form a valid triangle.")
This is an Isosceles Triangle (Two sides are equal).