有趣的python小程式
1.密碼生成器
x=int(input())
print(''.join(__import__('random').choice('QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm!@#$%^&*()_+=}{[]:;<,.>?/1234567890') for i in range(x)))
複制
運作結果:
16
IMHxl0+I_u8%)NnF
複制
2.時光機
import time
import datetime
import sys
print('輸入指定日期即可穿越(隻能到未來)'"\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 winsound
tone={'1':532,'2':588,'3':660,'4':698,'5':784,'6':880,'7':988}
while True:
winsound.Beep(tone[input()],300)
複制