search
HomeBackend DevelopmentPython TutorialRecommended collection, five Python mini projects (with source code)

Recommended collection, five Python mini projects (with source code)

In the process of using Python, what I like most is Python’s various third-party libraries, which can complete many operations.

The following will introduce you to 5 projects built through Python to learn Python programming.

1. Rock, Paper, Scissors Game

Objective: Create a command line game where players can choose between rock, scissors and paper to compete with the computer. If the player wins, points are added until the end of the game, when the final score is displayed to the player.

Tip: Receive the player's choice and compare it with the computer's choice. The computer's selection is chosen randomly from a selection list. If the player wins, 1 point is added.

import random
choices = [Rock, Paper, Scissors]
computer = random.choice(choices)
player = False
cpu_score = 0
player_score = 0
while True:
 player = input(Rock, Paper orScissors?).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(fCPU:{cpu_score})
 print(fPlaer:{player_score})
 break
 else:
 print(That's not a valid play. Check your spelling!)
 computer = random.choice(choices)

2. Random password generator

Goal: Create a program that can specify the password length and generate a string of random passwords.

Tip: Create a string of numbers, uppercase letters, lowercase letters, and special characters. Randomly generate a string of passwords based on the set password length.

import random
passlen = int(input(enter the length of password ))
s= abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKL MNOPQRSTUVIXYZ!aN$x*6*( )?
p = .join(random.sample(s,passlen ))
print(p)
----------------------------
enter the length of password
6
Za1gB0

3. Dice Simulator

Purpose: Create a program to simulate throwing dice.

Tip: Use the random module to generate a number between 1 and 6 when the user asks.

import random;
while int(input('Press 1 to roll the dice or 0 to exit:n')): print( random. randint(1,6))
--------------------------------------------------------------------
Press 1 to roll the dice or 0 to exit
1
4

4. Automatically send emails

Purpose: Write a Python script that can be used to send emails.

Tip: The email library can be used to send emails.

import smtplib
from email.message import EmailMessage
email = EmailMessage() ## Creating a object for EmailMessage
email['from'] = 'xyz name' ## Person who is sending
email['to'] = 'xyz id' ## Whom we are sending
email['subject'] = 'xyz subject'## Subject of email
email.set_content(Xyz content of email) ## content of email
with smtlib.SMTP(host='smtp.gmail.com',port=587)as smtp:
## sending request to server
 smtp.ehlo()## server object
smtp.starttls()## used to send data between server and client
smtp.login(email_id,Password) ## login id and password of gmail
smtp.send_message(email) ## Sending email
print(email send)## Printing success message

5. Alarm clock

Purpose: Write a Python script to create an alarm clock.

Tip: You can use the date-time module to create an alarm clock, and the playsound library to play sounds.

from datetime import datetime 
from playsound import playsound
alarm_time = input(Enter the time of alarm to be set:HH:MM:SSn)
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') ## download the alarm sound from link
break

The above is the detailed content of Recommended collection, five Python mini projects (with source code). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:51CTO.COM. If there is any infringement, please contact admin@php.cn delete
详细讲解Python之Seaborn(数据可视化)详细讲解Python之Seaborn(数据可视化)Apr 21, 2022 pm 06:08 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

详细了解Python进程池与进程锁详细了解Python进程池与进程锁May 10, 2022 pm 06:11 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

Python自动化实践之筛选简历Python自动化实践之筛选简历Jun 07, 2022 pm 06:59 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

归纳总结Python标准库归纳总结Python标准库May 03, 2022 am 09:00 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于标准库总结的相关问题,下面一起来看一下,希望对大家有帮助。

分享10款高效的VSCode插件,总有一款能够惊艳到你!!分享10款高效的VSCode插件,总有一款能够惊艳到你!!Mar 09, 2021 am 10:15 AM

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

python中文是什么意思python中文是什么意思Jun 24, 2019 pm 02:22 PM

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

Python数据类型详解之字符串、数字Python数据类型详解之字符串、数字Apr 27, 2022 pm 07:27 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

详细介绍python的numpy模块详细介绍python的numpy模块May 19, 2022 am 11:43 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools