0% found this document useful (0 votes)
2 views14 pages

Python Programs for Traffic, Grades, and More

...

Uploaded by

yATHARTH Tyagi
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)
2 views14 pages

Python Programs for Traffic, Grades, and More

...

Uploaded by

yATHARTH Tyagi
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

Gaurav Satyawali MCA AI & DS) Sec-A 25210820077/ [Link].

27

PROBLEM STATEMENT 1 : Write a Python simulator to model a traffic signal system for a
smart city.

•The traffic signal cycles through 3 lights:

o Green for 5 seconds

o Yellow for 2 seconds

o Red for 7 seconds

• This cycle repeats indefinitely.

• For a given time duration n seconds, print which light is ON at each second.

This problem helps understand loops with modulo (%) arithmetic.

Testcase 1:

Sample Input

Enter total seconds to simulate: 12

Sample Output

Time 1s → Green

Time 2s → Green

Time 3s → Green

Time 4s → Green

Time 5s → Green

Time 6s → Yellow

Time 7s → Yellow

Time 8s → Red

Time 9s → Red

Time 10s → Red

Time 11s → Red

Time 12s → Red

Testcase 2:

Sample Input

Enter total seconds to simulate: 23

1
Gaurav Satyawali MCA AI & DS) Sec-A 25210820077/ [Link].27

Sample Output

Time 1s → Green

Time 2s → Green

Time 3s → Green

Time 4s → Green

Time 5s → Green

Time 6s → Yellow

Time 7s → Yellow

Time 8s → Red

Time 9s → Red

Time 10s → Red

Time 11s → Red

Time 12s → Red

Time 13s → Red

Time 14s → Red

Time 15s → Green

Time 16s → Green

Time 17s → Green

Time 18s → Green

Time 19s → Green

Time 20s → Yellow

Time 21s → Yellow

Time 22s → Red

Time 23s → Red

2
Gaurav Satyawali MCA AI & DS) Sec-A 25210820077/ [Link].27

CODE :

n = int(input("Enter total seconds to simulate: "))

cycle = 14

for t in range(1, n + 1):

pos = (t - 1) % cycle + 1

if pos <= 5:

light = "Green"

elif pos <= 7:

light = "Yellow"

else:

light = "Red"

print(f"Time {t}s → {light}")

OUTPUT :

3
Gaurav Satyawali MCA AI & DS) Sec-A 25210820077/ [Link].27

PROBLEM STATEMENT 2 : Write a python program to calculate the average


marks of a student from 5 subjects and determine their grade based on the
average.
 if average >=90 (Grade A)
 elif average >=80 (Grade B)
 elif average >=70 (Grade C)
 elif average >=60 (Grade D)
 else average <60 (Fail)

Test case:
Enter marks: 100,98,95,99,88

CODE :

n1 = int(input("Enter your marks = "))

n2 = int(input("Enter your marks = "))

n3 = int(input("Enter your marks = "))

n4 = int(input("Enter your marks = "))

n5 = int(input("Enter your marks = "))

average = (n1 + n2 + n3 + n4 + n5) / 5

if average >= 90:

print("Grade A")

elif average >= 80:

print("Grade B")

elif average >= 70:

print("Grade C")

elif average >= 60:

print("Grade D")

else:

print("Fail")

4
Gaurav Satyawali MCA AI & DS) Sec-A 25210820077/ [Link].27

OUTPUT :

5
Gaurav Satyawali MCA AI & DS) Sec-A 25210820077/ [Link].27

PROBLEM STATEMENT 3 : Write a python program that:


 Accepts an integer input from the user.
 Checks wether the number is an Armstrong
number.
 Checks whether the number is an Armstrong number.
 Prints “Armstrong Number” if true, else “Not
an armstrong number”:

Test case:
1. Enter your number =200
Output= not an Armstrong number
2. Enter your number
=153 Output=
Armstrong number

CODE :

num = int(input("Enter your number = "))

n = num

digits = len(str(num))

total = 0

while n > 0:

digit = n % 10

total += digit ** digits

n //= 10

if total == num:

print("Armstrong Number")

else:

print("Not an Armstrong number")

6
Gaurav Satyawali MCA AI & DS) Sec-A 25210820077/ [Link].27

OUTPUT :

PROBLEM STATEMENT 4 : Design a pyhton program to simulate ATM login.

7
Gaurav Satyawali MCA AI & DS) Sec-A 25210820077/ [Link].27

A user is allowed 3 attempts to enter the correct PIN.

Test Case 1:
Enter PIN: 1234
Access Granted
✅ Test Case 2:
Enter PIN: 2134
Wrong PIN ❌
Attempts left: 2
Enter PIN: 2323
Wrong PIN ❌
Attempts left: 1
Enter PIN: 1235
Wrong PIN ❌
Attempts left: 0
Account Locked

CODE :

correct_pin = "1234"

attempts = 3

while attempts > 0:

pin = input("Enter PIN: ")

if pin == correct_pin:

print("Access Granted ✅")

break

else:

attempts -= 1

if attempts == 0:

print("Wrong PIN ❌ Attempts left: 0")

print("Account Locked 🔒")

else:

print(f"Wrong PIN ❌ Attempts left: {attempts}")

8
Gaurav Satyawali MCA AI & DS) Sec-A 25210820077/ [Link].27

OUTPUT :

PROBLEM STATEMENT 5 : Write a Python simulator to monitor a patient’s


hourly temperature in a [Link] hospital continuously monitors a
patient’s health by recording body temperature once every hour. To ensure
timely medical intervention, the system should detect cases of sustained

9
Gaurav Satyawali MCA AI & DS) Sec-A 25210820077/ [Link].27

high fever.
• The hospital records temperature once every hour.
• If the patient’s temperature exceeds 100°F for 3
consecutive hours, the system should trigger an alert.
• The program should:
Input the total number of hours to be monitored. Accept
hourly temperature readings from the user.
If the temperature exceeds 100°F for three consecutive hours, trigger an
alert.
• The program should:
 Print the first time interval (hours) when the alert condition
occurs.
 If no such condition is found, display “No alert required”.
 Example Testcases:

 Testcase1:
 Sample Input:
 Enter number of hours to monitor: 12
 Enter temperature at hour 0: 100
 Enter temperature at hour 1: 120
 Enter temperature at hour 2: 99
 Enter temperature at hour 3: 120
 Enter temperature at hour 4: 123
 Enter temperature at hour 5: 231
 Enter temperature at hour 6: 98
 Enter temperature at hour 7: 100
 Enter temperature at hour 8: 121
 Enter temperature at hour 9: 99
 Enter temperature at hour 10: 121
 Enter temperature at hour 11: 124

 Sample Output:
 Alert! High fever detected from hour 3 to hour 5

CODE :

10
Gaurav Satyawali MCA AI & DS) Sec-A 25210820077/ [Link].27

n = int(input("Enter number of hours to monitor: "))

temps = []

for i in range(n):

t = float(input(f"Enter temperature at hour {i}: "))

[Link](t)

alert_triggered = False

for i in range(n - 2):

if temps[i] > 100 and temps[i+1] > 100 and temps[i+2] > 100:

print(f"Alert! High fever detected from hour {i} to hour {i+2}")

alert_triggered = True

break

if not alert_triggered:

print("No alert required")

OUTPUT :

PROBLEM STATEMENT 6: Design a python program to simulate an e-


commerce discount engine for an online shopping platform. The program
should calculate the discount and final payable amount based on the

11
Gaurav Satyawali MCA AI & DS) Sec-A 25210820077/ [Link].27

following rules:
 If total purchase amount is Rs. 10,000 or more, give a 20%
discount.
 If total purchase amount is Rs. 5000 or more
but less than Rs. 10,000 give a 10% discount.
 If total purchase amount is less than Rs. 5000, no discount is
given.
 If the payment method is credit card and the
original purchase amount is Rs.8000 or more,
provide an additional RS. 500 cashback after
discount.

Test case: 1-Enter total purchase amount: 1200


Enter payment method (Cash/Credit Card/UPI): credit card Discount:
Rs. 0
Final payable amount: Rs. 1200.0

2- Enter total purchase amount: 12000


Enter payment method (Cash/Credit Card/UPI): credit card Discount:
Rs. 2400.0
Final payable amount: Rs. 9100.0

3- Enter total purchase amount: 7500


Enter payment method (Cash/Credit Card/UPI): cash
Discount: Rs. 750.0
Final payable amount: Rs. 6750.0

CODE :

amount = float(input("Enter total purchase amount: "))

method = input("Enter payment method (Cash/Credit Card/UPI): ").lower()

if amount >= 10000:

discount = 0.20 * amount

elif amount >= 5000:

discount = 0.10 * amount

else:
12
Gaurav Satyawali MCA AI & DS) Sec-A 25210820077/ [Link].27

discount = 0

final_amount = amount - discount

if method == "credit card" and amount >= 8000:

final_amount -= 500

print(f"Discount: Rs. {discount}")

print(f"Final payable amount: Rs. {final_amount}")

OUTPUT :

PROBLEM STATEMENT 7: Write a program to calculate the electricity bill for


a consumer based on number of units consumed. Charges as follows:

13
Gaurav Satyawali MCA AI & DS) Sec-A 25210820077/ [Link].27

 For the first 100 units : Rs. 5 per unit.

 For the next 100 units (i.e., 101-200): Rs 7 per unit.

 For consumption above 200 units : Rs 10 per unit.

Additionally, if total bill exceeds Rs. 2000, a surcharge of 5% is applied on


the bill amount.

CODE :

units = int(input("Enter number of units consumed: "))

if units <= 100:

bill = units * 5

elif units <= 200:

bill = 100 * 5 + (units - 100) * 7

else:

bill = 100 * 5 + 100 * 7 + (units - 200) * 10

if bill > 2000:

bill += bill * 0.05

print("Total Bill = Rs.", bill)

OUTPUT :

14

Common questions

Powered by AI

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 .

You might also like