一个值得学习练手的5个Python迷你程序(附代码
2022-04-26 本文已影响0人
程序员小西
人生苦短,快学Python!
在使用 Python 的过程中,我最喜欢的就是 Python 的各种库,能够完成很多操作。
下面就给大家介绍通过Python制作的项目,来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(fCPU:{cpu_score})
print(fPlaer:{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
三、选择子
目的:创建一个程序来模拟掷骰子。
提示:当用户了解的时候,使用随机模块生成一个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
四、自动发送邮件
目的:编写一个Python脚本,可以使用这个脚本发送电子邮件。
提示:电子邮件库可用于发送电子邮件。
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的。
提示:你可以使用日期播放声音库创建声音。
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