有趣的python小程序
1.密码生成器
x=int(input())print(''.join(__import__('random').choice('QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm!@#$%^&*()_+=}{[]:;<,.>?/1234567890') for i in range(x)))
运行结果:
16IMHxl0+I_u8%)NnF
2.时光机
import timeimport datetimeimport sysprint('输入指定日期即可穿越(只能到未来)'"\n"'请输入目标年月日:')y=int(input('年'))m=int(input('月'))d=int(input('日'))sj=str(y)+' '+str(m)+' '+str(d)d1=datetime.datetime(y,m,d)print('启动中,还需要')while True: d2=datetime.datetime.now() sec=round((d1-d2).total_seconds()) op=[int(sec/86400),'天',int((sec-int(sec/86400)*86400)/3600),'小时',int((sec-int(sec/3600)*3600)/60),'分',int((sec-int(sec/60)*60)),'秒'] nn=(''.join('%s' %id for id in op)) sys.stdout.write("\r%s"%nn) sys.stdout.write('即可启动') sys.stdout.flush() time.sleep(1)
运行结果:
输入指定日期即可穿越(只能到未来)请输入目标年月日:年2018月9日5启动中,还需要6天1小时55分3秒即可启动
3.QQ群机器人
需安装qqbot模块,图灵机器人最好自己申请一个。@机器人可以聊天,两个空格再加文本可自动翻译
import urllib.parse
import urllib.request
import requests,json
from qqbot import QQBotSlot as qqbotslot,RunBot
@qqbotslot
def onQQMessage(bot,contact,member,content):
def save(name,content):
with open(name, 'w') as f:
f.write(content)
def tuling(info):
url = 'http://www.tuling123.com/openapi/api?key=d846628468214520b1047b1ed0038fb1'+'&info='+info
res = requests.get(url)
res.encoding = 'utf-8'
jd = json.loads(res.text)
return jd['text']
if '@ME' in content or '@Fabot' in content:#Fabot是我给机器人起的名字
bot.SendTo(contact,tuling(content[4:]))
if(content[0]==' '):
url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&sessionFrom=http://fanyi.youdao.com/'
data = {
'i': content[1:],
'doctype': 'json',
}
data=urllib.parse.urlencode(data).encode('utf-8')
response=urllib.request.urlopen(url,data)
html=response.read().decode('utf-8')
target=json.loads(html)
bot.SendTo(contact,target['translateResult'][0][0]['tgt'])
RunBot()
运行结果:
4.弹钢琴
import winsoundtone={'1':532,'2':588,'3':660,'4':698,'5':784,'6':880,'7':988}while True: winsound.Beep(tone[input()],300)