Very Interesting Python Projects
1. Calculator using Functions
def add(x, y): return x + y
def subtract(x, y): return x - y
def multiply(x, y): return x * y
def divide(x, y): return x / y if y != 0 else "Undefined"
def calculator():
print("Select operation: +, -, *, /")
op = input("Operation: ")
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
operations = {'+': add, '-': subtract, '*': multiply, '/': divide}
result = operations.get(op, lambda x, y: "Invalid operation")(a, b)
print("Result:", result)
calculator()
2. Number Guessing Game
import random
def guess_game():
number = random.randint(1, 100)
attempts = 0
print("Guess a number between 1 and 100")
while True:
guess = int(input("Your guess: "))
attempts += 1
if guess < number:
print("Too low!")
elif guess > number:
print("Too high!")
else:
print(f"Congratulations! You guessed it in {attempts} tries.")
break
guess_game()
3. Basic To-Do List CLI
tasks = []
def show_tasks():
if not tasks:
print("No tasks yet.")
for i, task in enumerate(tasks, 1):
print(f"{i}. {task}")
def add_task(task):
tasks.append(task)
print("Task added.")
def remove_task(index):
try:
removed = tasks.pop(index - 1)
print(f"Removed task: {removed}")
except IndexError:
print("Invalid task number.")
while True:
print("\n1. Show Tasks\n2. Add Task\n3. Remove Task\n4. Exit")
choice = input("Choose an option: ")
if choice == '1':
show_tasks()
elif choice == '2':
add_task(input("Enter task: "))
elif choice == '3':
remove_task(int(input("Enter task number to remove: ")))
elif choice == '4':
break
else:
print("Invalid choice.")
4. Simple Chatbot
def chatbot():
print("Hi, I am a simple chatbot. Type 'bye' to exit.")
while True:
user = input("You: ").lower()
if user == 'bye':
print("Bot: Goodbye!")
break
elif "hello" in user:
print("Bot: Hello there!")
elif "how are you" in user:
print("Bot: I'm just code, but I'm fine!")
else:
print("Bot: I didn't understand that.")
chatbot()
5. Multiplication Table Generator
def multiplication_table(n):
print(f"Multiplication table for {n}:")
for i in range(1, 11):
print(f"{n} x {i} = {n * i}")
number = int(input("Enter a number: "))
multiplication_table(number)
6. Countdown Timer
import time
def countdown(seconds):
while seconds:
mins, secs = divmod(seconds, 60)
timeformat = f'{mins:02}:{secs:02}'
print(timeformat, end='\r')
time.sleep(1)
seconds -= 1
print("Time's up!")
countdown(10)
7. Rock Paper Scissors Game
import random
def rps():
options = ["rock", "paper", "scissors"]
while True:
user = input("Choose rock, paper or scissors (or exit): ").lower()
if user == "exit":
break
if user not in options:
print("Invalid choice.")
continue
computer = random.choice(options)
print("Computer chose:", computer)
if user == computer:
print("It's a tie!")
elif (user == "rock" and computer == "scissors") or (user
== "scissors" and computer == "paper") or (user == "paper" and
computer == "rock"):
print("You win!")
else:
print("You lose.")
rps()
8. Basic Alarm Clock
import datetime
import time
import os
def alarm_clock(alarm_time):
print(f"Alarm set for {alarm_time}")
while True:
now = datetime.datetime.now().strftime("%H:%M")
if now == alarm_time:
print("Wake up! Alarm ringing...")
os.system("echo Alarm!") # Use a beep sound on your OS
break
time.sleep(30)
alarm = input("Set alarm (HH:MM): ")
alarm_clock(alarm)
9. Simple Digital Clock
import time
def digital_clock():
while True:
print(time.strftime("%H:%M:%S"), end="\r")
time.sleep(1)
digital_clock()
10. Email Validator
import re
def is_valid_email(email):
pattern = r'^[a-z0-9]+[._]?[a-z0-9]+[@]\w+\.(com|edu|net)$'
return re.match(pattern, email)
email = input("Enter email to validate: ")
if is_valid_email(email):
print("Valid email.")
else:
print("Invalid email.")