在使用Python的過程中,我最喜歡的就是Python的各種第三方函式庫,能夠完成很多操作。
以下就跟大家介紹5個透過Python建構的項目,以學習Python程式設計。
目標:創建一個命令列遊戲,遊戲者可以在石頭、剪刀和布之間進行選擇,與電腦PK。如果遊戲者贏了,得分就會被添加,直到結束遊戲時,最終的分數會顯示給遊戲者。
提示:接收遊戲者的選擇,並且與電腦的選擇進行比較。計算機的選擇是從選擇清單中隨機選取的。如果遊戲者獲勝,則增加1分。
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)
目標:建立一個程序,可指定密碼長度,產生一串隨機密碼。
提示:建立一個數字 大寫字母 小寫字母 特殊字元的字串。根據設定的密碼長度隨機產生一串密碼。
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
目的:建立一個程式來模擬擲骰。
提示:當使用者詢問時,使用random模組產生一個1到6之間的數字。
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
目的:寫一個Python腳本,可以使用這個腳本傳送電子郵件。
提示: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
目的:編寫一個創建鬧鐘的Python腳本。
提示:你可以使用date-time模組來建立鬧鐘,以及playsound庫播放聲音。
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
以上是建議收藏,五個Python迷你項目(附源碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!