五个Python迷你版小程序附代码

 更新时间:2021年11月18日 10:36:13   作者:Python学习与数据挖掘  
在使用Python的过程中,我最喜欢的就是Python的各种第三方库,能够完成很多操作。下面就给大家介绍5个通过 Python 构建的实战项目,来实践 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: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 游戏内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 对pandas的层次索引与取值的新方法详解

    对pandas的层次索引与取值的新方法详解

    今天小编就为大家分享一篇对pandas的层次索引与取值的新方法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-11-11
  • Python 使用input同时输入多个数的操作

    Python 使用input同时输入多个数的操作

    这篇文章主要介绍了Python 使用input同时输入多个数的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • Python模块包中__init__.py文件功能分析

    Python模块包中__init__.py文件功能分析

    这篇文章主要介绍了Python模块包中__init__.py文件功能,简单分析了__init__.py在调入模块和包的过程中的作用,需要的朋友可以参考下
    2016-06-06
  • python实现数据预处理之填充缺失值的示例

    python实现数据预处理之填充缺失值的示例

    下面小编就为大家分享一篇python实现数据预处理之填充缺失值的示例。具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12
  • Python绘制散点密度图的三种方式详解

    Python绘制散点密度图的三种方式详解

    散点密度图是在散点图的基础上,计算了每个散点周围分布了多少其他的点,并通过颜色表现出来。本文主要介绍了Python绘制散点密度图的三种方式,需要的可以参考下
    2022-06-06
  • 浅析Python WSGI的使用

    浅析Python WSGI的使用

    WSGI也称之为web服务器通用网关接口,全称是web server gateway interface。这篇文章主要为大家介绍了Python WSGI的使用,希望对大家有所帮助
    2023-04-04
  • python爬虫爬取某网站视频的示例代码

    python爬虫爬取某网站视频的示例代码

    这篇文章主要介绍了python爬虫爬取某网站视频的示例代码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-02-02
  • Django ORM 多表查询示例代码

    Django ORM 多表查询示例代码

    这篇文章主要介绍了Django ORM 多表查询,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-09-09
  • Python 多张图片合并成一个pdf的参考示例

    Python 多张图片合并成一个pdf的参考示例

    最近需要将记的笔记整理成一个pdf进行保存,所以就研究了一下如何利用 Python 代码将拍下来的照片整个合并成一个pdf
    2021-06-06
  • python实现批量压缩指定目录下的文件夹

    python实现批量压缩指定目录下的文件夹

    这篇文章主要介绍了利用Python实现批量压缩指定目录下的文件夹的示例代码,文中代码示例讲解详细,感兴趣的小伙伴快跟随小编一起动手试一试
    2023-08-08

最新评论