赞
踩
目录
Hello!大家好哇!我是努力赚钱买生发水的灰小猿!
最近在做Python项目开发的时候愣是发现了好多好玩的小项目,并且都是代码量较少容易上手的,所以今天就来和小伙伴分享一些Python从一行代码到三十行代码都有哪些好玩的项目。之后大灰狼也会在这里持续更新更多好玩的项目。
print('\n'.join([''.join([('lovelovelove'[(x-y)%12]if((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3<=0 else' ')for x in range(-30,30)])for y in range(15,-15,-1)]))
效果如下:
- from MyQR import myqr
- myqr.run(words='https://hao.360.com/',picture='Sources/gakki.gif',save_name='qr4.png',colorized=True)
效果如下:
- from wordcloud import WordCloud
- cy = WordCloud() # 创建词云对象
- cy.generate('HuiXiaoYuan look forward to making progress with you') # 生成词云
- cy.to_file('cy.png') # 保存词云
效果如下:
- import turtle
- t = turtle.Pen()
- for x in range(360):
- t.forward(x)
- t.left(59)
效果如下:
- from turtle import *
- color('red', 'yellow')
- begin_fill()
- while True:
- forward(200)
- left(170)
- if abs(pos()) < 1:
- break
- end_fill()
- done()
效果如下:
- checkerboard = {'top-L': ' ', 'top-M': ' ', 'top-R': ' ',
- 'mid-L': ' ', 'mid-M': ' ', 'mid-R': ' ',
- 'low-L': ' ', 'low-M': ' ', 'low-R': ' '}
-
- def printBoard(replace):
- print(checkerboard['top-L'] + '|' + checkerboard['top-M'] + '|' + checkerboard['top-R'])
-
- print('——' + '+' + '——')
- print(checkerboard['mid-L'] + '|' + checkerboard['mid-M'] + '|' + checkerboard['mid-R'])
- print('——' + '+' + '——')
- print(checkerboard['low-L'] + '|' + checkerboard['low-M'] + '|' + checkerboard['low-R'])
- turn = 'X'
- for i in range(9):
- printBoard(checkerboard)
- print("现在请" + turn + "出棋,请输入您的位置:")
- move = input()
- checkerboard[move] = turn
- if turn == 'X':
- turn = 'O'
- else:turn = 'X'
- if i == 8:
- print("游戏结束,平棋!")
效果如下:
- from PIL import Image
- IMG = 't01b2a945701805d7f1.jpg' #设置图片文件
- WIDTH = 150 #设置字符画的宽
- HEIGHT = 80 #设置字符画的高
- OUTPUT = 'output5.txt' #设置存放字符画的文本文件
- ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ") #设置显示的字符集
- def get_char(r,g,b,alpha = 256):
- if alpha == 0:
- return ' '
- length = len(ascii_char)
- gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b)
- unit = (255.0 + 1)/length
- return ascii_char[int(gray/unit)]
- if __name__ == '__main__':
- im = Image.open(IMG)
- im = im.resize((WIDTH,HEIGHT), Image.NEAREST)
- txt = ""
- for i in range(HEIGHT):
- for j in range(WIDTH):
- txt += get_char(*im.getpixel((j,i)))
- txt += '\n'
- print(txt)
- with open(OUTPUT,'w') as f:
- f.write(txt)
效果如下:
- import requests
- import re
- import time
- url = "http://image.baidu.com/search/index?tn=baiduimage&word=皮卡丘"
- urls = requests.get(url) # 打开链接
- urltext = urls.text # 获取链接全部文本
- urlre = re.compile('"objURL":"(.*?)"', re.S) # 书写正则表达式
- urllist = urlre.findall(urltext) # 通过正则进行匹配
-
- with open("1.txt", "w") as txt: # 将匹配到的链接写入文件
- for i in urllist:
- txt.write(i + "\n")
- i = 0
-
- # 循环遍历列表并下载图片
- for urlimg in urllist:
- time.sleep(3) # 程序休眠三秒
- img = requests.get(urlimg, timeout = 5).content # 以二进制形式打开图片链接
- if img:
- with open(str(i) + ".jpg", "wb") as imgs: # 新建一个jpg文件,以二进制写入
- print("正在下载第%s张图片 %s" % (str(i+1), urlimg))
- imgs.write(img) #将图片写入
- i += 1
- if i == 3: #为了避免无限下载,在这里设定下载图片为3张
- break
- else:
- print("下载失败!")
-
- print("下载完毕!")
效果如下:
个别项目讲解链接附上:
觉得有用记得点赞关注哟!
同时你也可以关注我的微信公众号“灰狼洞主”,回复 “Python笔记”获取Python从入门到精通笔记分享和常用函数方法速查手册!
大灰狼期待与你一同进步!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。