日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
值得學(xué)習(xí)練手的五個(gè)Python迷你程序(附代碼)

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

創(chuàng)新互聯(lián)專注于廣陵網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供廣陵?duì)I銷型網(wǎng)站建設(shè),廣陵網(wǎng)站制作、廣陵網(wǎng)頁(yè)設(shè)計(jì)、廣陵網(wǎng)站官網(wǎng)定制、小程序開(kāi)發(fā)服務(wù),打造廣陵網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供廣陵網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。

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

一、石頭剪刀布游戲

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

提示:接收游戲者的選擇,并且與計(jì)算機(jī)的選擇進(jìn)行比較。計(jì)算機(jī)的選擇是從選擇列表中隨機(jī)選取的。如果游戲者獲勝,則增加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) 

二、隨機(jī)密碼生成器

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

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

 
 
 
  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. 6  
  9. Za1gB0 

三、骰子模擬器

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

提示:當(dāng)用戶詢問(wèn)時(shí),使用random模塊生成一個(gè)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. 1  

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

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

提示:email庫(kù)可用于發(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 

五、鬧鐘

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

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

 
 
 
  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  

分享標(biāo)題:值得學(xué)習(xí)練手的五個(gè)Python迷你程序(附代碼)
URL地址:http://www.dlmjj.cn/article/cohsgpo.html