前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python趣味代码

Python趣味代码

作者头像
何叶
发布2021-08-24 15:12:07
4730
发布2021-08-24 15:12:07
举报
文章被收录于专栏:何叶的专栏何叶的专栏

某博主最近不知道该写什么文章,最近Python深得许多爬虫大佬的喜欢,于是某闲着么事干的博主就搜罗了一些Python趣味代码。

可以百度搜索“Python在线编辑器”,不需要安装本地环境也能编译运行代码。

No.1 爱心

代码语言:javascript
复制
print('\n'.join([''.join([('Love'[(x-y) % len('Love')] 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(30, -30, -1)]))

No.2 99乘法表

代码语言:javascript
复制
print('\n'.join([' '.join(['%s*%s=%-2s' % (y, x, x*y) for y in range(1, x+1)]) for x in range(1, 10)]))

No.3 石头剪刀布

代码语言:javascript
复制
# coding=utf-8
import random
# 获胜次数
win_times = 0
while win_times <= 2:
  player = input('请输入:剪刀(0)  石头(1)  布(2):')
  player = int(player)
  computer = random.randint(0,2)
 
  if ((player == 0) and (computer == 2)) or ((player ==1) and (computer == 0)) or ((player == 2) and (computer == 1)):
      win_times += 1
      print('获胜,哈哈,你太厉害了')
  elif player == computer:
      print('平局,要不再来一局')
  else:
      print('输了,不要走,洗洗手接着来,决战到天亮')

No.4 猜数字

代码语言:javascript
复制
#!/usr/local/bin/python3

import random

bot=int(input('Set range bottom\n'))

top=int(input('Set range top\n'))

rand=random.randint(bot,top)

print ('Random number in ['+str(bot)+','+str(top)+'] generated!')

num=int(input('###Guess the number###\n'))

cnt=1

while (num!=rand):

if (num<rand):

print('*_* Lower than the answer')

else:

print('T_T Higher than the answer')

num=int(input('###Guess the number###\n'))

cnt=cnt+1

print('^_^ You get the answer with [%d] times'%cnt)

结语

引用别人的内容还写这么少,太过分了!

Python的学习过程是痛苦快乐的,希望你在今后的学习中学到更多好玩的知识。

引用内容:①风变编程&酱酱 ②简书&小天真_5eeb https://www.jianshu.com/p/0519365d27c8 侵删


版权属于:何叶

本文链接:https://cloud.tencent.com/developer/article/1867026

本站采用 “署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5 CN)” 许可。 您可转载本站文章,请以超链接形式标明本文原始出处、作者信息以及版权声明。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019 年 12 月,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • No.1 爱心
  • No.2 99乘法表
  • No.3 石头剪刀布
  • No.4 猜数字
  • 结语
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档