值得练手的5个 Python 迷你项目

一、石头剪刀布游戏

目标:创建一个命令行游戏,游戏者可以在石头、剪刀和布之间进行选择,与计算机PK。如果游戏者赢了,得分就会添加,直到结束游戏时,最终的分数会展示给游戏者。
提示:接收游戏者的选择,并且与计算机的选择进行比较。计算机的选择是从选择列表中随机选取的。如果游戏者获胜,则增加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) 

二、随机密码生成器

目标:创建一个程序,可指定密码长度,生成一串随机密码。
提示:创建一个数字+大写字母+小写字母+特殊字符的字符串。根据设定的密码长度随机生成一串密码。
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 

三、骰子模拟器

目的:创建一个程序来模拟掷骰子。
提示:当用户询问时,使用random模块生成一个1到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 

四、自动发送邮件

目的:编写一个Python脚本,可以使用这个脚本发送电子邮件。
提示:email库可用于发送电子邮件。
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 

五、闹钟

目的:编写一个创建闹钟的Python脚本。
提示:你可以使用date-time模块创建闹钟,以及playsound库播放声音。
from datetime import datetime     
from playsound import playsound  
alarm_time = input(“Enter the time of alarm to be set:HH:MM:SSn”)  
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  

 

原创文章,作者:guozi,如若转载,请注明出处:https://www.sudun.com/ask/79280.html

(0)
guozi's avatarguozi
上一篇 2024年5月30日 下午6:17
下一篇 2024年5月30日 下午6:21

相关推荐

  • 如何防止网站被入侵,网站如何防御ddos攻击

    互联网行业的发展带来了便利,但技术的不断进步也让网站保护成为必不可少的要素。但是你知道什么是网站防御吗?为什么需要设置网站防御呢?今天我们就来讨论一下当你受到攻击时如何配置网站防御…

    行业资讯 2024年5月15日
    0
  • C++中如何使用布尔类型(bool)?

    C++中的布尔类型(bool)是什么?它与网络互联网服务器行业又有着怎样的关系呢?在这个信息爆炸的时代,我们每天都在使用网络互联网,但是对于服务器行业的朋友们来说,C++中的布尔类…

    行业资讯 2024年4月17日
    0
  • 域名的关系

    在网络行业中,有一个重要的概念经常被提及,那就是“域名”。它似乎与网站、品牌形象等词汇有着密切的关系,但究竟是怎样的关系呢?今天我们就来探讨一下“域名”的关系。从什么是域名开始,我…

    行业资讯 2024年4月11日
    0
  • 网址被攻击,网站被攻击有什么表现

    3、数据异常 如果您发现网站数据异常增加或减少,例如页面浏览量突然下降或消息数量突然增加,则该网站可能受到攻击。黑客可以通过更改您的数据或删除重要信息来损坏您的网站。 4. 警告信…

    行业资讯 2024年5月9日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注