欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

五個Python迷你版小程序附代碼

 更新時間:2021年11月18日 10:36:13   作者:Python學習與數(shù)據(jù)挖掘  
在使用Python的過程中,我最喜歡的就是Python的各種第三方庫,能夠完成很多操作。下面就給大家介紹5個通過 Python 構建的實戰(zhàn)項目,來實踐 Python 編程能力。歡迎收藏學習,喜歡點贊支持

一、石頭剪刀布游戲

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

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

二、隨機密碼生成器

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

提示:創(chuàng)建一個數(shù)字+大寫字母+小寫字母+特殊字符的字符串。根據(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)建一個程序來模擬擲骰子。

提示:當用戶詢問時,使用random模塊生成一個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

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

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

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

五、鬧鐘

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

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

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

技術交流

歡迎轉載、收藏、有所收獲點贊支持一下!

在這里插入圖片描述

到此這篇關于五個Python迷你版小游戲附代碼的文章就介紹到這了,更多相關Python 游戲內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • python游戲實戰(zhàn)項目之智能五子棋

    python游戲實戰(zhàn)項目之智能五子棋

    下五子棋嗎?信不信我讓你幾步你也贏不了?本篇為你帶來用python編寫的五子棋小游戲,文中給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值
    2021-09-09
  • Python?使用和高性能技巧操作大全

    Python?使用和高性能技巧操作大全

    這篇文章主要介紹了Python?使用和高性能技巧總結,對一些python易混淆的操作進行對比,不少 Python 的用戶是從以前 C/C++ 遷移過來的,這兩種語言在語法、代碼風格等方面有些不同,本節(jié)簡要進行介紹,需要的朋友可以參考下
    2022-01-01
  • Python深度學習pytorch神經網(wǎng)絡多輸入多輸出通道

    Python深度學習pytorch神經網(wǎng)絡多輸入多輸出通道

    這篇文章主要為大家介紹了Python深度學習中pytorch神經網(wǎng)絡多輸入多輸出通道的詳解有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2021-10-10
  • Linux下Python安裝完成后使用pip命令的詳細教程

    Linux下Python安裝完成后使用pip命令的詳細教程

    這篇文章主要介紹了Linux下Python安裝完成后使用pip命令的詳細教程,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-11-11
  • OpenCV中圖像通道操作的深入講解

    OpenCV中圖像通道操作的深入講解

    圖像處理管道是一組按預定義順序執(zhí)行的任務,用于將圖像轉換為所需的結果或提取一些有趣的特征,下面這篇文章主要給大家介紹了關于OpenCV中圖像通道操作的相關資料,需要的朋友可以參考下
    2021-10-10
  • pytorch獲取模型某一層參數(shù)名及參數(shù)值方式

    pytorch獲取模型某一層參數(shù)名及參數(shù)值方式

    今天小編就為大家分享一篇pytorch獲取模型某一層參數(shù)名及參數(shù)值方式,具有很好的價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • 淺析python常用數(shù)據(jù)文件處理方法

    淺析python常用數(shù)據(jù)文件處理方法

    這篇文章主要介紹了python常用數(shù)據(jù)文件處理方法,雖說python運行速度慢,但其編程速度,第三方包的豐富度是真的高,涉及到文件批處理還是會選擇python,感興趣的朋友跟隨小編一起看看吧
    2021-10-10
  • 解讀Python中degrees()方法的使用

    解讀Python中degrees()方法的使用

    這篇文章主要介紹了Python中degrees()方法的使用,是Python入門中的基礎知識,需要的朋友可以參考下
    2015-05-05
  • Python numpy  數(shù)組的向量化運算操作方法

    Python numpy  數(shù)組的向量化運算操作方法

    這篇文章主要介紹了Python numpy數(shù)組的向量化運算操作方法,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-06-06
  • 使用python 計算百分位數(shù)實現(xiàn)數(shù)據(jù)分箱代碼

    使用python 計算百分位數(shù)實現(xiàn)數(shù)據(jù)分箱代碼

    這篇文章主要介紹了使用python 計算百分位數(shù)實現(xiàn)數(shù)據(jù)分箱代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03

最新評論