Python 的功能强大,可以应用到你生活中的各个角落,接下来介绍运用广泛的五个小项目。
一、邮件地址切片器
目标:写一个 Python 脚本,能从邮件地址获取用户名和域名
tips:使用 @ 作为分隔符,把地址分成2个字符串
二、自动发送邮件
目标:做一个可以自动发送电子邮件的脚本
tips:email 库可用于发送电子邮件。
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
三、缩写词
目标:做一个可以把给定句子生成缩写词的脚本
tips:可以通过索引和拆分来获得第一个单词再将其组合
四、文字冒险游戏
目标:做一个能通过路径选择不同的选项让用户进行有趣的冒险
五、Hangman 游戏
目标:做出一个命令行 Hangman 游戏
tips:创建密码词列表并随机选择一个单词,将每个单词用下划线"_"表示,提供用户猜单词的机会,如果猜对会用"_"将单词替换。
import time
import random
name = input("What is your name? ")
print ("Hello, " + name, "Time to play hangman!")
time.sleep(1)
print ("Start guessing...\n")
time.sleep(0.5)
## A List Of Secret Words
words = ['python','programming','treasure','creative','medium','horror']
word = random.choice(words)
guesses = ''
turns = 5
while turns > 0:
failed = 0
for char in word:
if char in guesses:
print (char,end="")
else:
print ("_",end=""),
failed += 1
if failed == 0:
print ("\nYou won")
break
guess = input("\nguess a character:")
guesses += guess
if guess not in word:
turns -= 1
print("\nWrong")
print("\nYou have", + turns, 'more guesses')
if turns == 0:
print ("\nYou Lose")
以上就是小编为您带来的关于 Python 的五个迷你项目 的全部内容。