Python 的功能強大,可以應(yīng)用到你生活中的各個角落,接下來介紹運用廣泛的五個小項目。
一、郵件地址切片器
目標:寫一個 Python 腳本,能從郵件地址獲取用戶名和域名
tips:使用 @ 作為分隔符,把地址分成2個字符串
二、自動發(fā)送郵件
目標:做一個可以自動發(fā)送電子郵件的腳本
tips:email 庫可用于發(fā)送電子郵件。
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:創(chuàng)建密碼詞列表并隨機選擇一個單詞,將每個單詞用下劃線"_"表示,提供用戶猜單詞的機會,如果猜對會用"_"將單詞替換。
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")
以上就是小編為您帶來的關(guān)于 Python 的五個迷你項目 的全部內(nèi)容。