五个Python迷你版小程序附代码
作者:Python学习与数据挖掘 发布时间:2023-09-28 02:55:26
标签: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
技术交流
欢迎转载、收藏、有所收获点赞支持一下!
来源:https://blog.csdn.net/weixin_38037405/article/details/121378595


猜你喜欢
- 本文实例讲述了Python实现读取邮箱中的邮件功能。分享给大家供大家参考,具体如下:#-*- encoding: utf-8 -*-impo
- 本文实例讲述了Python实现将HTML转成PDF的方法。分享给大家供大家参考,具体如下:主要使用的是wkhtmltopdf的Python封
- 看例子: 数 据表 collect ( id, title ,info ,vtype) 就这4个字段,其中 title 用定长,info 用
- 一、ts文件的由来ts文件,ts即"Transport Stream"的缩写,特点就是要求从视频流的任一片段开始都是可以
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- 原作者:Nik Piepenbreier翻译&内容补充:费弗里原文地址:https://towardsdatascience.com
- 本文给大家分享的是查看MySQL连接的root密码的方法,下面话不多说来来看正文:1.首先我们进到MySQL的bin目录下➜ cd /usr
- 本文主要分享的是一则python+opencv实现任意角度的透视变换的实例,具体如下:# -*- coding:utf-8 -*-impor
- 一 引入解释器在执行到定义变量的语法时,会申请内存空间来存放变量的值,而内存的容量是有限的,这就涉及到变量值所占用内存空间的回收问题,当一个
- 目录准备xlrd 读取 Excelxlwt 写入 Excel进阶用法最后准备使用 Python 操作 Excel 文件,常见的方式如下:xl
- 粉丝提问今天粉丝提了下面这样一个问题,其中一个是"一行拆多行",另外一个是"多行并一行",貌似群友用
- 前言“幸运牛牛套圈圈”套住欢乐,圈住幸福,等你来挑战!哈喽,大家上午好,我是你们的栗子同学,今天来给
- 本文实例讲述了python计算N天之后日期的方法。分享给大家供大家参考。具体如下:python计算N天之后的日期,可以自己写成一个函数,想得
- 来蓝色一直都在潜水,很少写帖子,太对不起大家了.下面和大家探讨一个话题,希望能引起大家的兴趣.关于H1,一直都想和大家讨论H1用法的问题,可
- 在程序设计中,鸭子类型(英语:duck typing)是动态类型的一种风格。在这种风格中,一个对象有效的语义,不是由继承自特定的类或实现特定
- 很多时候,我发现自己需要进行生成报告、输出文件或字符串的任务。它们或多或少都会遵循某种模式,通常这些模式是如此相似,以至于我们希望拥有一个可
- django settings.py 配置文件import osBASE_DIR = os.path.dirname(os.path.dir
- 这篇文章主要介绍了pandas 空数据处理方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可
- xlsxwriter可能用过的人并不是很多,不过使用后就会感觉,他的功能让你叹服,除了可以按要求生成你所需要的excel外还可以加上很形象的
- 示例from optparse import OptionParser[...]def main():