您好,欢迎访问一九零五行业门户网

建议收藏,五个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 orscissors?).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
三、骰子模拟器目的:创建一个程序来模拟掷骰子。
提示:当用户询问时,使用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:ssn) 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迷你项目(附源码)的详细内容。
其它类似信息

推荐信息