[go: up one dir, main page]

0% found this document useful (0 votes)
8 views6 pages

sample answer_python practice 1 (1)

Answers to python questions

Uploaded by

Zain Ali Rizvi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

sample answer_python practice 1 (1)

Answers to python questions

Uploaded by

Zain Ali Rizvi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

✅ 1.

Age Check for Voting Eligibility


Basic Version:
age = int(input("Enter your age: "))
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote yet.")

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."

age = int(input("Enter your age: "))


print(check_voting_eligibility(age))

Console Example:
Enter your age: 17
You are not eligible to vote yet.

✅ 2. Parking Fee Calculator


Simple Version:
hours = int(input("Enter parking hours: "))
fee = hours * 50
print("Total parking fee is:", fee, "PKR")

Using Function:
def calculate_parking_fee(hours):
return hours * 50

hours = int(input("Enter parking hours: "))


print("Total parking fee is:", calculate_parking_fee(hours), "PKR")

Console Example:
Enter parking hours: 3
Total parking fee is: 150 PKR

✅ 3. Movie Ticket Booking


Basic Version:
age = int(input("Enter your age: "))
if age < 12:
price = 300
elif age <= 60:
price = 600
else:
price = 400
print("Your ticket price is:", price, "PKR")

Using Function:
def get_ticket_price(age):
if age < 12:
return 300
elif age <= 60:
return 600
else:
return 400

age = int(input("Enter your age: "))


print("Your ticket price is:", get_ticket_price(age), "PKR")

Console Example:
Enter your age: 65
Your ticket price is: 400 PKR

✅ 4. Basic Calculator (Add or Subtract)


Simple Version:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
operation = input("Enter operation (add/subtract): ")

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"

marks = int(input("Enter marks: "))


print("Grade:", get_grade(marks))

Console Example:
Enter marks: 78
Grade: B

✅ 6. Water Usage Charge


Basic Version:
liters = int(input("Enter water used (in liters): "))
if liters <= 100:
bill = liters * 10
else:
bill = (100 * 10) + ((liters - 100) * 15)
print("Water bill is:", bill, "PKR")

Using Function:
def calculate_water_bill(liters):
if liters <= 100:
return liters * 10
else:
return (100 * 10) + (liters - 100) * 15

liters = int(input("Enter water used (in liters): "))


print("Water bill is:", calculate_water_bill(liters), "PKR")

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."

temp = float(input("Enter current temperature: "))


print(check_weather(temp))

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}: ")))

print("Total bill is:", calculate_total(items), "PKR")

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

✅ 9. Bus Seat Booking


Simple Version:
available = 10
book = int(input("How many seats do you want to book? "))
if book <= available:
available -= book
print("Booking confirmed. Seats left:", available)
else:
print("Not enough seats available.")

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

✅ 10. Table of a Number


Using for loop:
num = int(input("Enter a number: "))
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")

Using Function:
def print_table(n):
for i in range(1, 11):
print(f"{n} x {i} = {n * i}")

num = int(input("Enter a number: "))


print_table(num)

Console Example:
Enter a number: 5
5 x 1 = 5
5 x 2 = 10
...
5 x 10 = 50

You might also like