Python Practice Exercises
Exercise 1: Grade Calculator
marks = [int(input("Enter mark for subject")) for _ in range(5)]
total = sum(marks)
average = total / 5
if average >= 90:
grade = 'A'
elif average >= 80:
grade = 'B'
elif average >= 70:
grade = 'C'
elif average >= 60:
grade = 'D'
else:
grade = 'F'
print("Total:", total)
print("Average:", average)
print("Grade:", grade)
Exercise 2: Simple ATM Machine
pin = 1234
balance = 1000
entered_pin = int(input("Enter your PIN: "))
if entered_pin == pin:
print("1. Check Balance\n2. Deposit\n3. Withdraw")
choice = int(input("Choose option: "))
if choice == 1:
print("Balance:", balance)
elif choice == 2:
amount = float(input("Enter deposit amount: "))
balance += amount
print("New Balance:", balance)
elif choice == 3:
amount = float(input("Enter withdraw amount: "))
if amount <= balance:
balance -= amount
print("New Balance:", balance)
else:
print("Message:", "Insufficient funds")
else:
print("Message:", "Invalid choice")
else:
print("Message:", "Wrong PIN")
Exercise 3: Login System
username = "admin"
password = "1234"
u = input("Username: ")
p = input("Password: ")
if u == username and p == password:
print("Login:", "Successful")
else:
print("Login:", "Failed")
Exercise 4: Even or Odd Checker
for i in range(5):
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Number is", "Even")
else:
print("Number is", "Odd")
Exercise 5: BMI Calculator
weight = float(input("Enter weight in kg: "))
height = float(input("Enter height in meters: "))
bmi = weight / (height ** 2)
if bmi < 18.5:
status = "Underweight"
elif bmi < 25:
status = "Normal"
elif bmi < 30:
status = "Overweight"
else:
status = "Obese"
print("BMI:", round(bmi, 2))
print("Status:", status)
Exercise 6: Traffic Signal Simulation
color = input("Enter signal color (red/yellow/green): ").lower()
if color == "red":
print("Message:", "Stop")
elif color == "yellow":
print("Message:", "Slow Down")
elif color == "green":
print("Message:", "Go")
else:
print("Message:", "Invalid color")
Exercise 7: Hotel Room Booking System
nights = int(input("Enter number of nights: "))
room_type = input("Enter room type (Standard/Deluxe/Suite): ").lower()
if room_type == "standard":
rate = 100
elif room_type == "deluxe":
rate = 150
elif room_type == "suite":
rate = 200
else:
rate = 0
if rate:
total = nights * rate
print("Total cost:", total)
else:
print("Message:", "Invalid room type")
Exercise 8: Voting Eligibility Checker
age = int(input("Enter age: "))
if age >= 18:
print("Eligibility:", "Eligible to vote")
else:
print("Eligibility:", "Not eligible to vote")
Exercise 9: Leap Year Checker
year = int(input("Enter year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap Year:", True)
else:
print("Leap Year:", False)
Exercise 10: Basic Calculator with Menu
print("Choose operation: +, -, *, /")
op = input("Enter operator: ")
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
if op == '+':
result = a + b
elif op == '-':
result = a - b
elif op == '*':
result = a * b
elif op == '/':
result = a / b
else:
result = "Invalid operation"
print("Result:", result)
Exercise 11: Train Ticket Price Calculator
age = int(input("Enter age: "))
distance = float(input("Enter distance: "))
if age < 5:
fare = 0
elif age > 60:
fare = (distance * 5) * 0.5
else:
fare = distance * 5
print("Ticket Price:", fare)
Exercise 12: Temperature Converter
print("1. Celsius to Fahrenheit\n2. Fahrenheit to Celsius")
choice = int(input("Choose conversion: "))
temp = float(input("Enter temperature: "))
if choice == 1:
converted = (temp * 9/5) + 32
print("Converted Temperature:", converted)
elif choice == 2:
converted = (temp - 32) * 5/9
print("Converted Temperature:", converted)
else:
print("Message:", "Invalid choice")