Home > Backend Development > Python Tutorial > Recommended collection, five Python mini projects (with source code)

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

WBOY
Release: 2023-05-20 16:04:07
forward
1456 people have browsed it

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)
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template