五个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)
python可以做什么游戏
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("", 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
ssage 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='ail',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 = w()
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 游戏内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论