sample answer_python practice 1 (1)
sample answer_python practice 1 (1)
Using Function:
def check_voting_eligibility(age):
if age >= 18:
return "You are eligible to vote."
else:
return "You are not eligible to vote yet."
Console Example:
Enter your age: 17
You are not eligible to vote yet.
Using Function:
def calculate_parking_fee(hours):
return hours * 50
Console Example:
Enter parking hours: 3
Total parking fee is: 150 PKR
Using Function:
def get_ticket_price(age):
if age < 12:
return 300
elif age <= 60:
return 600
else:
return 400
Console Example:
Enter your age: 65
Your ticket price is: 400 PKR
if operation == "add":
result = a + b
elif operation == "subtract":
result = a - b
else:
result = "Invalid operation"
print("Result:", result)
Using Function:
def calculate(a, b, op):
if op == "add":
return a + b
elif op == "subtract":
return a - b
else:
return "Invalid"
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
op = input("Enter operation (add/subtract): ")
print("Result:", calculate(a, b, op))
Console Example:
Enter first number: 8
Enter second number: 3
Enter operation (add/subtract): subtract
Result: 5
✅ 5. Grade Calculator
Simple Version:
marks = int(input("Enter marks out of 100: "))
if marks >= 90:
grade = "A"
elif marks >= 75:
grade = "B"
elif marks >= 60:
grade = "C"
else:
grade = "F"
print("Grade:", grade)
Using Function:
def get_grade(marks):
if marks >= 90:
return "A"
elif marks >= 75:
return "B"
elif marks >= 60:
return "C"
else:
return "F"
Console Example:
Enter marks: 78
Grade: B
Using Function:
def calculate_water_bill(liters):
if liters <= 100:
return liters * 10
else:
return (100 * 10) + (liters - 100) * 15
Console Example:
Enter water used (in liters): 120
Water bill is: 1300 PKR
✅ 7. Temperature Checker
Simple Version:
temp = float(input("Enter current temperature in Celsius: "))
if temp > 35:
print("It's hot outside.")
elif temp < 15:
print("It's cold outside.")
else:
print("The weather is pleasant.")
Function Version:
def check_weather(temp):
if temp > 35:
return "It's hot outside."
elif temp < 15:
return "It's cold outside."
else:
return "The weather is pleasant."
Console Example:
Enter current temperature: 22
The weather is pleasant.
✅ 8. Shopping Cart Total
Using Loop:
total = 0
for i in range(5):
price = float(input(f"Enter price of item {i+1}: "))
total += price
print("Total bill is:", total, "PKR")
Using Function:
def calculate_total(prices):
return sum(prices)
items = []
for i in range(5):
items.append(float(input(f"Enter price of item {i+1}: ")))
Console Example:
Enter price of item 1: 100
Enter price of item 2: 200
Enter price of item 3: 300
Enter price of item 4: 50
Enter price of item 5: 150
Total bill is: 800.0 PKR
Using Function:
def book_seats(available, requested):
if requested <= available:
return True, available - requested
else:
return False, available
available = 10
requested = int(input("How many seats do you want to book? "))
status, seats_left = book_seats(available, requested)
if status:
print("Booking confirmed. Seats left:", seats_left)
else:
print("Not enough seats available.")
Console Example:
How many seats do you want to book? 4
Booking confirmed. Seats left: 6
Using Function:
def print_table(n):
for i in range(1, 11):
print(f"{n} x {i} = {n * i}")
Console Example:
Enter a number: 5
5 x 1 = 5
5 x 2 = 10
...
5 x 10 = 50