Python 的功能強(qiáng)大,可以應(yīng)用到你生活中的各個(gè)角落,接下來(lái)介紹運(yùn)用廣泛的五個(gè)小項(xiàng)目。
一、郵件地址切片器
目標(biāo):寫一個(gè) Python 腳本,能從郵件地址獲取用戶名和域名
tips:使用 @ 作為分隔符,把地址分成2個(gè)字符串
二、自動(dòng)發(fā)送郵件
目標(biāo):做一個(gè)可以自動(dòng)發(fā)送電子郵件的腳本
tips:email 庫(kù)可用于發(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
三、縮寫詞
目標(biāo):做一個(gè)可以把給定句子生成縮寫詞的腳本
tips:可以通過(guò)索引和拆分來(lái)獲得第一個(gè)單詞再將其組合
四、文字冒險(xiǎn)游戲
目標(biāo):做一個(gè)能通過(guò)路徑選擇不同的選項(xiàng)讓用戶進(jìn)行有趣的冒險(xiǎn)
五、Hangman 游戲
目標(biāo):做出一個(gè)命令行 Hangman 游戲
tips:創(chuàng)建密碼詞列表并隨機(jī)選擇一個(gè)單詞,將每個(gè)單詞用下劃線"_"表示,提供用戶猜單詞的機(jī)會(huì),如果猜對(duì)會(huì)用"_"將單詞替換。
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")
以上就是小編為您帶來(lái)的關(guān)于 Python 的五個(gè)迷你項(xiàng)目 的全部?jī)?nèi)容。