自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

值得學(xué)習(xí)練手的五個Python迷你程序(附代碼)

開發(fā) 后端
下面就給大家介紹5個通過Python構(gòu)建的項目,以此來學(xué)習(xí)Python編程。

[[426416]]

 在使用Python的過程中,我最喜歡的就是Python的各種第三方庫,能夠完成很多操作。

下面就給大家介紹5個通過Python構(gòu)建的項目,以此來學(xué)習(xí)Python編程。

一、石頭剪刀布游戲

目標(biāo):創(chuàng)建一個命令行游戲,游戲者可以在石頭、剪刀和布之間進行選擇,與計算機PK。如果游戲者贏了,得分就會添加,直到結(jié)束游戲時,最終的分?jǐn)?shù)會展示給游戲者。

提示:接收游戲者的選擇,并且與計算機的選擇進行比較。計算機的選擇是從選擇列表中隨機選取的。如果游戲者獲勝,則增加1分。 

  1. import random  
  2. choices = ["Rock", "Paper", "Scissors"]  
  3. computer = random.choice(choices)  
  4. player = False  
  5. cpu_score = 0  
  6. player_score = 0  
  7. while True:  
  8.     player = input("Rock, Paper or  Scissors?").capitalize()  
  9.     # 判斷游戲者和電腦的選擇  
  10.     if player == computer:  
  11.         print("Tie!")  
  12.     elif player == "Rock":  
  13.         if computer == "Paper":  
  14.             print("You lose!", computer, "covers", player)  
  15.             cpu_score+=1  
  16.         else:  
  17.             print("You win!", player, "smashes", computer)  
  18.             player_score+=1  
  19.     elif player == "Paper":  
  20.         if computer == "Scissors":  
  21.             print("You lose!", computer, "cut", player)  
  22.             cpu_score+=1 
  23.         else:  
  24.             print("You win!", player, "covers", computer)  
  25.             player_score+=1  
  26.     elif player == "Scissors":  
  27.         if computer == "Rock":  
  28.             print("You lose...", computer, "smashes", player)  
  29.             cpu_score+=1  
  30.         else:  
  31.             print("You win!", player, "cut", computer)  
  32.             player_score+=1  
  33.     elif player=='E':  
  34.         print("Final Scores:")  
  35.         print(f"CPU:{cpu_score}")  
  36.         print(f"Plaer:{player_score}")  
  37.         break  
  38.     else:  
  39.         print("That's not a valid play. Check your spelling!")  
  40.     computer = random.choice(choices) 

二、隨機密碼生成器

目標(biāo):創(chuàng)建一個程序,可指定密碼長度,生成一串隨機密碼。

提示:創(chuàng)建一個數(shù)字+大寫字母+小寫字母+特殊字符的字符串。根據(jù)設(shè)定的密碼長度隨機生成一串密碼。 

  1. import random  
  2. passlen = int(input("enter the length of password" ))  
  3. s=" abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKL MNOPQRSTUVIXYZ!aN$x*6*( )?"  
  4. p = ".join(random.sample(s,passlen ))  
  5. print(p)  
  6. ----------------------------  
  7. enter the length of password  
  8.  
  9. Za1gB0 

三、骰子模擬器

目的:創(chuàng)建一個程序來模擬擲骰子。

提示:當(dāng)用戶詢問時,使用random模塊生成一個1到6之間的數(shù)字。 

  1. import random;  
  2. while int(input('Press 1 to roll the dice or 0 to exit:\n')): print( random. randint(1,6))  
  3. --------------------------------------------------------------------  
  4. Press 1 to roll the dice or 0 to exit  
  5.  

四、自動發(fā)送郵件

目的:編寫一個Python腳本,可以使用這個腳本發(fā)送電子郵件。

提示:email庫可用于發(fā)送電子郵件。 

  1. import smtplib  
  2.  
  3. from email.message import EmailMessage 
  4.  
  5. email = EmailMessage() ## Creating a object for EmailMessage 
  6.  
  7. email['from'] = 'xyz name'   ## Person who is sending 
  8.  
  9. email['to'] = 'xyz id'       ## Whom we are sending 
  10.  
  11. email['subject'] = 'xyz subject'  ## Subject of email 
  12.  
  13. email.set_content("Xyz content of email") ## content of email 
  14.  
  15. with smtlib.SMTP(host='smtp.gmail.com',port=587)as smtp:      
  16.  
  17. ## sending request to server  
  18.  
  19.     smtp.ehlo()          ## server object 
  20.  
  21. smtp.starttls()      ## used to send data between server and client 
  22.  
  23. smtp.login("email_id","Password") ## login id and password of gmail 
  24.  
  25. smtp.send_message(email)   ## Sending email 
  26.  
  27. print("email send")    ## Printing success message 

五、鬧鐘

目的:編寫一個創(chuàng)建鬧鐘的Python腳本。

提示:你可以使用date-time模塊創(chuàng)建鬧鐘,以及playsound庫播放聲音。 

  1. from datetime import datetime     
  2. from playsound import playsound  
  3. alarm_time = input("Enter the time of alarm to be set:HH:MM:SS\n")  
  4. alarm_hour=alarm_time[0:2]  
  5. alarm_minute=alarm_time[3:5]  
  6. alarm_seconds=alarm_time[6:8]  
  7. alarm_period = alarm_time[9:11].upper()  
  8. print("Setting up alarm..")  
  9. while True:  
  10.     now = datetime.now()  
  11.     current_hour = now.strftime("%I")  
  12.     current_minute = now.strftime("%M") 
  13.     current_seconds = now.strftime("%S")  
  14.     current_period = now.strftime("%p")  
  15.     if(alarm_period==current_period):  
  16.         if(alarm_hour==current_hour):  
  17.             if(alarm_minute==current_minute):  
  18.                 if(alarm_seconds==current_seconds):  
  19.                     print("Wake Up!")  
  20.                     playsound('audio.mp3') ## download the alarm sound from link  
  21.                     break  

 

責(zé)任編輯:龐桂玉 來源: 馬哥Linux運維
相關(guān)推薦

2022-07-26 09:22:04

Python項目

2022-07-22 09:55:02

Python練手題

2021-02-02 15:58:02

Python爬蟲腳本

2023-07-27 08:00:00

代碼補全服務(wù)人工智能

2020-12-09 11:52:28

Python字符串代碼

2014-10-30 10:28:09

HTML5

2017-09-05 13:30:25

AWS深度學(xué)習(xí)Linux

2023-08-24 16:28:44

程序員工具

2022-02-10 08:00:00

開發(fā)編輯器軟件

2024-09-18 02:01:00

2014-10-23 08:56:42

開源項目C

2013-04-11 10:00:44

云計算項目開源Puppet

2022-04-24 11:28:59

區(qū)塊鏈NFT加密貨幣

2020-02-12 11:54:32

網(wǎng)絡(luò)戰(zhàn)模擬工具網(wǎng)絡(luò)攻擊網(wǎng)絡(luò)安全

2023-02-28 10:21:17

2024-09-18 00:00:10

2023-06-19 10:30:04

Python工具數(shù)據(jù)科學(xué)工具

2022-09-30 08:31:06

TypeScriptJavaScrip

2022-01-12 16:18:10

云趨勢公有云云計算

2021-10-08 13:56:19

物聯(lián)網(wǎng)工業(yè)物聯(lián)網(wǎng)IoT
點贊
收藏

51CTO技術(shù)棧公眾號