1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        建議收藏,5個(gè)Python迷你項(xiàng)目(附源碼)

        共 3925字,需瀏覽 8分鐘

         ·

        2022-03-08 18:00

        ↑?關(guān)注 + 星標(biāo)?,每天學(xué)Python新技能

        后臺(tái)回復(fù)【大禮包】送你Python自學(xué)大禮包


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

        下面就給大家介紹5個(gè)通過Python構(gòu)建的項(xiàng)目,以此來學(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分。
        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ī)生成一串密碼。
        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ù)字。
        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ā)送電子郵件。

        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ù)播放聲音。

        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

        推薦閱讀

        1. 技術(shù)變現(xiàn)哪家強(qiáng),Python爬蟲能稱王!

        2. Python 遠(yuǎn)程連接服務(wù)器,看這一篇就夠了!

        3. 好用且能兼職賺錢的自動(dòng)化工具 - Hamibot!

        4. 用Python制作可視化報(bào)表,這也太快了!


        您看此文用???分??秒,轉(zhuǎn)發(fā)只需1秒哦

        瀏覽 35
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            超碰大屌 | 午夜成人免费观看视频 | 成人三级片无码 | 日产精品久久久一区二区 | 别揉我的奶头一区二区三区 | 人人看人人摸人人草 | 男女日b片 | 激情国产操逼视频 | 亚洲骚逼 | 亚洲欧美色图区 |