CodeAlpha Python Programming Internship -
Project Report
Prepared by: Sandhiya
Task 1: Hangman Game
A text-based game where the player guesses a hidden word one letter at a time with a maximum of
6 incorrect guesses.
Code:
import random
# List of 5 predefined words
words = ["python", "apple", "computer", "school", "hangman"]
# Randomly choose a word
word = [Link](words)
# Create hidden word display
guessed_word = ["_"] * len(word)
# Store guessed letters
guessed_letters = []
# Number of incorrect guesses allowed
attempts = 6
print("Welcome to Hangman!")
while attempts > 0 and "_" in guessed_word:
print("\nWord:", " ".join(guessed_word))
print("Incorrect guesses left:", attempts)
guess = input("Enter a letter: ").lower()
# Check if already guessed
if guess in guessed_letters:
print("You already guessed that letter!")
continue
guessed_letters.append(guess)
# Check if letter exists in word
if guess in word:
print("Correct!")
for i in range(len(word)):
if word[i] == guess:
guessed_word[i] = guess
else:
print("Wrong guess!")
attempts -= 1
# Final result
if "_" not in guessed_word:
print("\nCongratulations! You guessed the word:", word)
else:
print("\nGame Over!")
print("The word was:", word)
output
Task 2: Calculator
A Python calculator application that performs basic arithmetic operations such as addition,
subtraction, multiplication, and division.
Output
def calculator():
print("Simple Calculator")
print("Operations:")
print("1. Addition (+)")
print("2. Subtraction (-)")
print("3. Multiplication (*)")
print("4. Division (/)")
while True:
choice = input("\nEnter operation (1/2/3/4) or 'q' to quit: ")
if choice == 'q':
print("Calculator Closed.")
break
if choice in ('1', '2', '3', '4'):
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print("Result =", num1 + num2)
elif choice == '2':
print("Result =", num1 - num2)
elif choice == '3':
print("Result =", num1 * num2)
elif choice == '4':
if num2 != 0:
print("Result =", num1 / num2)
else:
print("Error! Division by zero is not allowed.")
else:
print("Invalid choice! Please try again.")
calculator()
output
Task 4: Basic Chatbot
Code
def chatbot():
print("Simple Chatbot")
print("Type 'bye' to exit.\n")
while True:
user_input = input("You: ").lower()
if user_input == "hello":
print("Bot: Hi!")
elif user_input == "how are you":
print("Bot: I'm fine, thanks!")
elif user_input == "bye":
print("Bot: Goodbye!")
break
else:
print("Bot: Sorry, I don't understand that.")
# Run the chatbot
chatbot()
output