Five Beginner Python Projects: Rock‑Paper‑Scissors Game, Random Password Generator, Dice Simulator, Email Sender, and Alarm Clock
This article introduces five beginner-friendly Python projects—a command‑line Rock‑Paper‑Scissors game, a random password generator, a dice‑rolling simulator, an automated email sender, and a simple alarm clock—each with clear objectives, implementation tips, and complete source code examples.
Python’s extensive third‑party libraries make it easy to build useful utilities quickly. Below are five small projects that illustrate core concepts such as user input handling, randomization, email communication, and time‑based scheduling.
1. Rock‑Paper‑Scissors Game
Goal: Create a command‑line game where the player chooses rock, paper, or scissors, competes against a computer‑selected move, and accumulates points until the player exits.
Tip: Use random.choice to pick the computer’s move and compare it with the player’s input.
import random
choices = ["Rock", "Paper", "Scissors"]
computer = random.choice(choices)
player = False
cpu_score = 0
player_score = 0
while True:
player = input("Rock, Paper or Scissors?").capitalize()
# 判断游戏者和电脑的选择
if player == computer:
print("Tie!")
elif player == "Rock":
if computer == "Paper":
print("You lose!", computer, "covers", player)
cpu_score += 1
else:
print("You win!", player, "smashes", computer)
player_score += 1
elif player == "Paper":
if computer == "Scissors":
print("You lose!", computer, "cut", player)
cpu_score += 1
else:
print("You win!", player, "covers", computer)
player_score += 1
elif player == "Scissors":
if computer == "Rock":
print("You lose...", computer, "smashes", player)
cpu_score += 1
else:
print("You win!", player, "cut", computer)
player_score += 1
elif player == 'E':
print("Final Scores:")
print(f"CPU:{cpu_score}")
print(f"Plaer:{player_score}")
break
else:
print("That's not a valid play. Check your spelling!")
computer = random.choice(choices)2. Random Password Generator
Goal: Generate a password of user‑specified length containing digits, uppercase and lowercase letters, and special characters.
Tip: Build a character pool string and use random.sample to pick unique characters.
import random
passlen = int(input("enter the length of password"))
s = " abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKL MNOPQRSTUVIXYZ!aN$x*6*( )?"
p = ".join(random.sample(s, passlen))
print(p)
# Example interaction
# enter the length of password
# 6
# Za1gB03. Dice Simulator
Goal: Simulate rolling a six‑sided die on user request.
Tip: Use random.randint(1,6) inside a loop that continues while the user inputs 1.
import random
while int(input('Press 1 to roll the dice or 0 to exit:\n')):
print(random.randint(1, 6))
# Example output
# Press 1 to roll the dice or 0 to exit:
# 1
# 44. Automatic Email Sender
Goal: Write a script that sends an email using Python’s email and smtplib libraries.
Tip: Create an EmailMessage object, fill the headers and content, then connect to an SMTP server with TLS.
import smtplib
from email.message import EmailMessage
email = EmailMessage()
email['from'] = 'xyz name'
email['to'] = 'xyz id'
email['subject'] = 'xyz subject'
email.set_content("Xyz content of email")
with smtplib.SMTP(host='smtp.gmail.com', port=587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.login("email_id", "Password")
smtp.send_message(email)
print("email send")5. Simple Alarm Clock
Goal: Create a Python script that triggers an alarm at a specified time.
Tip: Parse the user‑provided HH:MM:SS time, compare it with the current time in a loop, and play a sound using playsound when they match.
from datetime import datetime
from playsound import playsound
alarm_time = input("Enter the time of alarm to be set:HH:MM:SS\n")
alarm_hour = alarm_time[0:2]
alarm_minute = alarm_time[3:5]
alarm_seconds = alarm_time[6:8]
alarm_period = alarm_time[9:11].upper()
print("Setting up alarm..")
while True:
now = datetime.now()
current_hour = now.strftime("%I")
current_minute = now.strftime("%M")
current_seconds = now.strftime("%S")
current_period = now.strftime("%p")
if alarm_period == current_period:
if alarm_hour == current_hour:
if alarm_minute == current_minute:
if alarm_seconds == current_seconds:
print("Wake Up!")
playsound('audio.mp3')
breakThese examples demonstrate how Python can be leveraged for quick prototyping of games, utilities, and automation scripts, making it an ideal language for beginners to practice core programming concepts.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.