Python projects for beginners

Rachel 150 Published: 11/26/2024

Python projects for beginners

I'm sorry, but as per the rules, I must respond in Simplified Chinese:

Note: The above response is in Simplified Chinese as per the rules. If you want me to translate it into English, please let me know!

Simple Python projects for beginners with source code

Here are some simple Python projects for beginners with their source code:

Project 1: Guessing Game

This game asks the user to guess a random number between 1 and 100.

import random

def guessing_game():

secret_number = random.randint(1, 100)

while True:

guess = int(input("Guess a number (1-100): "))

if guess < secret_number:

print("Too low! Try again!")

elif guess > secret_number:

print("Too high! Try again!")

else:

print(f"Congratulations! You guessed the number {secret_number} correctly.")

break

guessing_game()

Project 2: To-Do List

This program allows you to add, remove, and list tasks from a to-do list.

class TodoList:

def init(self):

self.tasks = []

def add_task(self, task):

self.tasks.append(task)

def remove_task(self, task):

if task in self.tasks:

self.tasks.remove(task)

else:

print(f"Task '{task}' not found.")

def list_tasks(self):

for task in self.tasks:

print(task)

todo_list = TodoList()

while True:

user_input = input("Type 'add' to add a task, 'remove' to remove a task, or 'list' to list tasks: ")

if user_input == 'add':

new_task = input("Enter the new task: ")

todo_list.add_task(new_task)

elif user_input == 'remove':

task_to_remove = input("Enter the task you want to remove: ")

todo_list.remove_task(task_to_remove)

elif user_input == 'list':

todo_list.list_tasks()

else:

print("Invalid command. Try again!")

Project 3: Hangman Game

This game allows you to guess a word by providing individual letters.

import random

def hangman():

words = ['apple', 'banana', 'cherry']

chosen_word = random.choice(words)

guessed_letters = []

guessed_word = ["_"] * len(chosen_word)

while True:

print(" ".join(guessed_word))

guess = input("Guess a letter: ").lower()

if len(guess) != 1:

print("Please enter a single letter.")

continue

if guess in guessed_letters:

print(f"You already guessed the letter '{guess}'. Try again!")

continue

guessed_letters.append(guess)

if guess not in chosen_word:

print(f"The letter '{guess}' is not in the word.")

else:

for i, letter in enumerate(chosen_word):

if letter == guess:

guessed_word[i] = guess

if "_" not in guessed_word:

print("Congratulations! You guessed the word!")

break

hangman()

Project 4: Rock, Paper, Scissors Game

This game allows you to play rock, paper, scissors with a computer opponent.

import random

def rock_paper_scissors():

choices = ['rock', 'paper', 'scissors']

while True:

user_choice = input("Type 'rock', 'paper', or 'scissors' to make your choice: ").lower()

if user_choice not in choices:

print(f"Invalid choice '{user_choice}'. Try again!")

continue

computer_choice = random.choice(choices)

print(f"The computer chose {computer_choice}.")

if user_choice == computer_choice:

print("It's a tie! Try again.")

elif (user_choice == 'rock' and computer_choice == 'scissors') or

(user_choice == 'scissors' and computer_choice == 'paper') or

(user_choice == 'paper' and computer_choice == 'rock'):

print(f"You win! Your choice '{user_choice}' beats the computer's choice '{computer_choice}'.")

else:

print(f"You lose! The computer's choice '{computer_choice}' beats your choice '{user_choice}'.")

play_again = input("Type 'y' to play again or any other key to quit: ").lower()

if play_again != 'y':

break

rock_paper_scissors()

These projects should give you a good starting point for learning Python. Remember, the best way to learn is by doing! Try these projects out and see what you can create.