一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術(shù)及教程分享平臺(tái)!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - 五個(gè)Python迷你版小程序附代碼

五個(gè)Python迷你版小程序附代碼

2022-03-03 00:19Python學(xué)習(xí)與數(shù)據(jù)挖掘 Python

在使用Python的過程中,我最喜歡的就是Python的各種第三方庫(kù),能夠完成很多操作。下面就給大家介紹5個(gè)通過 Python 構(gòu)建的實(shí)戰(zhàn)項(xiàng)目,來實(shí)踐 Python 編程能力。歡迎收藏學(xué)習(xí),喜歡點(diǎn)贊支持

一、石頭剪刀布游戲

目標(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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import random
choices = ["Rock", "Paper", "Scissors"]
computer = random.choice(choices)
player = False
cpu_score = 0
player_score = 0
while True:
    player = input("Rock, Paper or  Scissors?").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(f"CPU:{cpu_score}")
        print(f"Plaer:{player_score}")
        break
    else:
        print("That's not a valid play. Check your spelling!")
    computer = random.choice(choices)

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

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

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

?
1
2
3
4
5
6
7
8
9
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

三、骰子模擬器

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

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

?
1
2
3
4
5
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

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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

五、鬧鐘

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from datetime import datetime  
from playsound import playsound
alarm_time = input("Enter the time of alarm to be set:HH:MM:SS\n")
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

技術(shù)交流

歡迎轉(zhuǎn)載、收藏、有所收獲點(diǎn)贊支持一下!

五個(gè)Python迷你版小程序附代碼

到此這篇關(guān)于五個(gè)Python迷你版小游戲附代碼的文章就介紹到這了,更多相關(guān)Python 游戲內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://blog.csdn.net/weixin_38037405/article/details/121378595

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 99精品偷自拍 | 国产一区二区精品 | 欧美一级欧美一级高清 | 亚洲国产精品线在线观看 | 被老外玩爽的中国美女视频 | 成人伊人青草久久综合网破解版 | 久久综合香蕉久久久久久久 | 太大了轻点阿受不了小说h 四色6677最新永久网站 | 91在线免费看 | 国产午夜亚洲精品不卡 | 国产婷婷成人久久av免费高清 | 无限资源在线观看8 | chanelpreston欧美网站 | 欧美视频在线播放观看免费福利资源 | 午夜影院网站 | 传说之下羊妈挤羊奶网站 | 男人的天堂日本 | 亚洲 欧美 国产 视频二区 | 色综合欧美色综合七久久 | 精品久久国产 | 星空传媒在线视频 | 成人欧美一区在线视频在线观看 | 大陆国产vs国产对白 | 日韩日韩日韩手机看片自拍 | 久久精品国产免费 | 性色生活片在线观看 | 特黄级| yy6080午夜国产免费福利 | dyav午夜片| 狠狠色成人综合 | 91伊人网| 精品久久久久久国产91 | 亚洲春黄在线观看 | 亚洲国产区男人本色在线观看欧美 | 青草社区视频 | 波多野结衣不卡 | 91视频破解版 | 日韩免费一级 | 久久久无码精品亚洲A片猫咪 | 色天使亚洲综合在线观看 | 国产成人亚洲精品91专区高清 |