五个Python迷你版小程序附代码
作者:Python学习与数据挖掘 时间:2023-09-28 02:55:26
一、石头剪刀布游戏
目标:创建一个命令行游戏,游戏者可以在石头、剪刀和布之间进行选择,与计算机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,小程序,源码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL数据库开启、关闭、查看函数功能的方法
2024-01-20 00:41:13
Laravel框架视图和模型操作方法分析
2024-05-11 09:19:35
品牌的统一体验
2010-05-19 13:08:00
![](https://img.aspxhome.com/file/UploadPic/20105/19/bue-01-70s.jpg)
python中id函数运行方式
2021-12-27 14:03:50
Python 如何实现变量交换
2021-07-03 12:48:20
Python中的变量及简单数据类型应用
2021-06-13 09:45:26
![](https://img.aspxhome.com/file/2023/5/71365_0s.png)
深入分析python中整型不会溢出问题
2022-11-20 01:19:13
避免重复写代码的小函数
2008-09-21 13:41:00
![](https://img.aspxhome.com/file/UploadPic/20089/21/2008921134758493s.gif)
Python3.7 读取 mp3 音频文件生成波形图效果
2022-09-08 18:02:25
![](https://img.aspxhome.com/file/2023/2/87662_0s.png)
解析smarty模板中类似for的功能实现
2023-11-15 12:53:40
Python命令行参数解析包argparse的使用详解
2021-04-01 01:58:52
![](https://img.aspxhome.com/file/2023/0/108060_0s.png)
python实现简单登陆系统
2023-05-25 06:50:49
教你怎么用Python实现GIF动图的提取及合成
2023-11-29 14:16:15
![](https://img.aspxhome.com/file/2023/3/102983_0s.gif)
vue3 watch和watchEffect的使用以及有哪些区别
2024-05-05 09:11:09
商业价值与用户价值的平衡
2008-12-10 18:42:00
![](https://img.aspxhome.com/file/UploadPic/200812/10/002_small-39s.jpg)
mysql数据库备份及恢复命令 mysqldump,source的用法
2024-01-22 21:05:10
php依赖注入知识点详解
2024-05-02 17:33:00
mysql 5.7.17 winx64解压版安装配置方法图文教程
2024-01-22 07:47:16
![](https://img.aspxhome.com/file/2023/0/70480_0s.png)
ASP伪静态页简单教程
2007-09-28 14:35:00
flask中的wtforms使用方法
2021-08-21 02:46:52
![](https://img.aspxhome.com/file/2023/3/114593_0s.png)