0% found this document useful (0 votes)
25 views

Class 8 Python worksheet

The document contains multiple Python programming exercises, including a student grade calculator, an electricity bill calculator, an online shopping discount system, and a traffic signal simulation. Each exercise provides criteria for calculations and the corresponding code implementations. The document serves as a practical guide for learning basic programming concepts and logic.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Class 8 Python worksheet

The document contains multiple Python programming exercises, including a student grade calculator, an electricity bill calculator, an online shopping discount system, and a traffic signal simulation. Each exercise provides criteria for calculations and the corresponding code implementations. The document serves as a practical guide for learning basic programming concepts and logic.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

WORKSHEET

Write the Output of the below given Code:


Question No 1 Question 3
x=2 a=5
y=5 while a >= 0:
print("x =", x) print(a)
print("y =", y) if a == 2:
for i in range(1,5): break
z=x*2+y a=a–1
x=y Output:
y=z
5
print("z =", z)
4
Output: 3
x=2 2
y=5
z = 75 Question 4
Question No 2 a = 10
p=1 while a >= 0:
q=6 print(a)
print("p =", p) if a == 7:
print("q =", q) break
for j in range(3): a=a–1
r=p+q*2 Output:
p=q
q=r 10
print("r =", r) 9
8
Output: 7
p=1
q=6
r = 91

1
Question 1: Student Grade Calculator
A school awards grades to students based on the following criteria:
• If the percentage is 90 or above, the grade is A+.
• If the percentage is 80-89, the grade is A.
• If the percentage is 70-79, the grade is B.
• If the percentage is 60-69, the grade is C.
• If the percentage is 50-59, the grade is D.
• If the percentage is below 50, the grade is Fail.
Write a Python program that takes five subject marks as input, calculates the percentage, and
assigns a grade based on the given conditions.
Answer:
# Taking input for marks in five subjects
sub1 = float(input("Enter marks for Subject 1: "))
sub2 = float(input("Enter marks for Subject 2: "))
sub3 = float(input("Enter marks for Subject 3: "))
sub4 = float(input("Enter marks for Subject 4: "))
sub5 = float(input("Enter marks for Subject 5: "))

# Calculating total and percentage


total_marks = sub1 + sub2 + sub3 + sub4 + sub5
percentage = (total_marks / 500) * 100 # Assuming each subject is out of 100

# Assigning grades based on percentage


if percentage >= 90:
grade = "A+"
elif percentage >= 80:
grade = "A"
elif percentage >= 70:
grade = "B"
elif percentage >= 60:
grade = "C"
elif percentage >= 50:
grade = "D"
else:
grade = "Fail"

# Displaying results
print("\nTotal Marks:", total_marks)
print("Percentage:", percentage)
print("Grade:", grade)
Question 2: Electricity Bill Calculator
A power distribution company calculates the monthly electricity bill of a customer based on
the units consumed using the following tariff:
• Up to 100 units → ₹5 per unit
• 101 to 300 units → ₹8 per unit
• 301 to 500 units → ₹10 per unit
• Above 500 units → ₹12 per unit
Additionally, if the total bill exceeds ₹3000, a 5% surcharge is added.
Write a Python program that takes the customer's name and number of units consumed as
input and calculates the total bill.

2
Answer:
# Taking inputs
name = input("Enter customer name: ")
units = int(input("Enter number of units consumed: "))

# Calculating bill based on unit consumption


if units <= 100:
bill = units * 5
elif units <= 300:
bill = (100 * 5) + ((units - 100) * 8)
elif units <= 500:
bill = (100 * 5) + (200 * 8) + ((units - 300) * 10)
else:
bill = (100 * 5) + (200 * 8) + (200 * 10) + ((units - 500) * 12)

# Applying surcharge if bill exceeds ₹3000


if bill > 3000:
surcharge = 0.05 * bill
bill += surcharge
else:
surcharge = 0

# Displaying bill details


print("\nElectricity Bill")
print("Customer Name:", name)
print("Units Consumed:", units)
print("Total Bill Amount: ₹", bill)
print("Surcharge Applied: ₹", surcharge)

Question 3: Online Shopping Discount System


An online shopping platform offers discounts based on the total purchase amount:
• If the purchase amount is ₹5000 or more, a 20% discount is applied.
• If the purchase amount is between ₹3000 and ₹4999, a 15% discount is applied.
• If the purchase amount is between ₹1000 and ₹2999, a 10% discount is applied.
• If the purchase amount is less than ₹1000, no discount is given.
Write a Python program that takes the total purchase amount as input, applies the appropriate
discount, and displays the final payable amount.
Answer:
# Taking input
total_amount = float(input("Enter total purchase amount: "))

# Applying discount based on purchase amount


if total_amount >= 5000:
discount = 0.20 * total_amount
elif total_amount >= 3000:
discount = 0.15 * total_amount
elif total_amount >= 1000:
discount = 0.10 * total_amount
else:

3
discount = 0

# Calculating final amount


final_amount = total_amount - discount

# Displaying results
print("\nShopping Bill Summary")
print("Total Purchase Amount: ₹", total_amount)
print("Discount Applied: ₹", discount)
print("Final Payable Amount: ₹", final_amount)

Question No 4: Write a Python program that simulates a traffic signal system. The program
should:
• Take the traffic light color as input from the user (red, yellow, or green).
• Display the corresponding action:
o Red → "Stop! Wait for the light to change."
o Yellow → "Slow down! Get ready to stop."
o Green → "Go! You can proceed."
• If the user enters an invalid color, display a message saying "Invalid input! Please
enter red, yellow, or green."

# Taking user input for traffic signal color


signal = input("Enter the traffic light color (red/yellow/green): ").strip().lower()

# Checking the signal color and displaying the appropriate message


if signal == "red":
print("Stop! Wait for the light to change.")
elif signal == "yellow":
print("Slow down! Get ready to stop.")
elif signal == "green":
print("Go! You can proceed.")
else:
print("Invalid input! Please enter red, yellow, or green.")

You might also like