Python Lab: Variables & Conditionals
Python Lab: Variables & Conditionals
To implement a leap year checker using conditional statements, you must check if the year is divisible by four but not by one hundred unless it is also divisible by four hundred. Specifically, the conditions are: if the year is divisible by 4 and not divisible by 100, or if it is divisible by 400, then it is a leap year .
A conditional system adapts bus fares by assessing age and destination zone, using base fare rates per zone (Zone 1: ₱20, Zone 2: ₱30, Zone 3: ₱40). Special age conditions offer adjustments: under 5s travel free, ages 5–18 receive a 50% discount, and over 60s a 30% discount. This necessitates branching logic that applies appropriate fares or additional reductions based on these distinct criteria .
Scholarship eligibility is determined using conditional statements evaluating average grade and family income. If a student’s average grade is 90 or above and family income is less than ₱200,000, they qualify for a full scholarship. If the average is 85 or higher, with income below ₱500,000, they qualify for a partial scholarship. Otherwise, the student is not eligible .
To calculate the total cost of items in an online shopping cart, multiply the quantity of each item by its unit price for individual totals, sum these to get the cart total, and format this data into a receipt. For example, use integers for quantities and floating-point numbers for unit prices to compute individual costs (e.g., 2 Apples x ₱50 = ₱100). Sum these for the total and display them in a formatted receipt style with a detailed list and final total underneath .
The triangle type identifier uses input of three side lengths to classify triangles. It checks the triangle inequality theorem: the sum of any two sides should be greater than the third side. If this fails, it’s 'Not a valid triangle'. If all sides are equal, it classifies as 'Equilateral'. If two sides are equal, it’s 'Isosceles'. If all sides differ, it’s 'Scalene' .
An ATM validates withdrawals by checking two conditions: the amount requested must be a multiple of 100, and it must not exceed the maximum limit of ₱20,000. If these conditions are met, the transaction is approved; if not, the system specifies the error, such as 'Invalid amount' or 'Exceeds limit' .
To convert a GPA into a letter grade, compare the GPA against predefined ranges: GPA of 3.6–4.0 translates to an 'A', 3.0–3.59 to 'B', 2.0–2.99 to 'C', 1.0–1.99 to 'D', and below 1.0 to 'F'. The conversion must ensure proper matching using logical bounds to assign the correct letter .
Challenges include ensuring data security and preventing unauthorized access. A basic system verifies login credentials by checking if the input matches preset conditions: username equals 'admin' and password equals '12345'. If these match, access is granted with 'Login Successful'; otherwise, it returns 'Invalid Credentials' .
An electricity bill is calculated by multiplying the kWh consumed by the rate per kWh (₱5 if ≤ 100, otherwise ₱8), then you add a fixed service charge of ₱50. This ensures correctness by clearly distinguishing between variable and non-variable charges while calculating the total billing amount .
The salary calculator must consider the employee's basic salary, overtime hours, and apply deductions. First, calculate overtime pay by multiplying the number of overtime hours by a fixed rate (e.g., ₱150 per hour). Then, sum the basic salary and overtime pay to find the gross salary. Apply a 12% tax on the gross salary to find taxable deductions and subtract this amount from the gross salary to determine the net salary. Results should be displayed neatly, formatted to two decimal places .