Python Programs for Traffic, Grades, and More
Python Programs for Traffic, Grades, and More
The electricity billing system calculates the charges based on three unit slabs: Rs. 5 per unit for the first 100, Rs. 7 per unit for the next 100, and Rs. 10 above 200 units. If the calculated bill surpasses Rs. 2000, a 5% surcharge is added. The program evaluates unit consumption, computes the bill according to slab rates, and applies this conditional surcharge if required .
The ATM login simulation program allows a user three attempts to enter the correct PIN. The correct PIN is predefined, and for each attempt, if the entered PIN matches the preset PIN, access is granted. If incorrect, an error message displays and the number of remaining attempts reduces by one. Once attempts exhaust, the account is locked .
In the discount engine program, if the total purchase amount is Rs. 10,000 or more, a 20% discount is applied; if between Rs. 5,000 and Rs. 10,000, a 10% discount. If less than Rs. 5,000, no discount is applied. Additionally, for purchases using a credit card, if the amount after discount is Rs. 8,000 or more, a further Rs. 500 cashback is granted. The final payable amount is the total after subtracting applicable discounts and any cashback .
A script verifies Armstrong numbers by checking if the sum of its own digits each raised to the power of the number's digit count matches the original number. It converts the number to a string to count digits, iterating over each digit, computing, and summing these powered values. Efficiently, it handles multiple numbers achievable through loops across various input cases .
To identify an Armstrong number, the program takes an integer input, calculates the number of digits, and computes the sum of each digit raised to the power of the number of digits. Specifically, the program iterates over each digit, performing the power calculation and accumulating the results. If the computed total equals the original number, it is an Armstrong number; otherwise, it is not .
The hospital's monitoring system triggers an alert when a patient’s temperature exceeds 100°F for three consecutive hours. The system takes input for the number of hours to monitor and records hourly temperature readings. If it detects three successive readings over 100°F, it triggers an alert specifying the first time interval where this condition occurs; if not, it displays 'No alert required' .
A student’s grade is determined as ‘Fail’ if the average of the marks from five subjects is less than 60. The grading system classifies grades as follows: Grade A for averages 90 and above, Grade B for averages between 80 and 89, Grade C for averages between 70 and 79, Grade D for averages between 60 and 69, and ‘Fail’ for averages below 60 .
To determine the current traffic light, use a loop with modulo arithmetic. The traffic signal in the simulation cycles through Green for 5 seconds, Yellow for 2 seconds, and Red for 7 seconds, leading to a cycle length of 14 seconds. For any given second 't', calculate 'pos' as (t - 1) % 14 + 1. If 'pos' is less than or equal to 5, the light is Green; if 'pos' is less than or equal to 7, it’s Yellow; otherwise, it's Red .
To apply and sequence rules, the monitoring program takes hourly temperature data inputs for a specified number of hours. It loops through these readings, checking if any three consecutive hours have temperatures exceeding 100°F. If true, it prints an alert with this specific interval. If the sequence fails at any check, the loop continues; if none are found, it concludes 'No alert required', ensuring real-time responsiveness and reliable alert conditions .
The simulator employs a cyclic approach using modulo arithmetic over a cycle of 14 seconds, composed of 5 seconds for Green, 2 for Yellow, and 7 for Red. By computing the position within this cycle with (t - 1) % 14 + 1 for each second 't', it determines the color: positions 1-5 map to Green, 6-7 to Yellow, and 8-14 to Red, repeating as 't' increases .