设为首页 收藏本站
查看: 1138|回复: 0

[经验分享] python基础小程序

[复制链接]

尚未签到

发表于 2018-8-9 06:14:34 | 显示全部楼层 |阅读模式
  ========================================
  猜大小的游戏
  #!/usr/bin/python
  # -*- coding: UTF-8 -*-
  import random
  s = int(random.uniform(1,100))
  #print(s)
  m = 1
  i = 0
  while m != s:
  m = int(input('输入你猜的整数:'))
  i += 1
  if m > s:
  print('你猜的数字大了,不要灰心,继续努力')
  if m < s:
  print('你猜的数字小了,不要灰心,继续加油')
  if m == s:
  if i <= 4:
  print('这么快就猜对了,太神了')
  elif i == 5:
  print('5次就猜对了,果然厉害')
  else :
  print i,&quot;次总算猜对了,恭喜恭喜&quot;
  break;
  ===============================
  ===============================
  猜拳小游戏
  #!/usr/bin/python
  # -*- coding: UTF-8 -*-
  import random
  while 1:
  s = int(random.randint(1, 3))
  if s == 1:
  ind = &quot;石头&quot;
  elif s == 2:
  ind = &quot;剪子&quot;
  elif s == 3:
  ind = &quot;布&quot;
  m = raw_input('输入 石头、剪子、布,输入&quot;end&quot;结束游戏:')
  blist = ['石头', &quot;剪子&quot;, &quot;布&quot;]
  if (m not in blist) and (m != 'end'):
  print &quot;输入错误,请重新输入!&quot;
  elif (m not in blist) and (m == 'end'):
  print &quot;\n游戏退出中...&quot;
  break
  elif m == ind :
  print &quot;电脑出了: &quot; + ind + &quot;,平局!&quot;
  elif (m == '石头' and ind =='剪子') or (m == '剪子' and ind =='布') or (m == '布' and ind =='石头'):
  print &quot;电脑出了: &quot; + ind +&quot;,你赢了!&quot;
  elif (m == '石头' and ind =='布') or (m == '剪子' and ind =='石头') or (m == '布' and ind =='剪子'):
  print &quot;电脑出了: &quot; + ind +&quot;,你输了!&quot;
  ===============================
  ===============================
  摇筛子游戏
  #!/usr/bin/env python3
  # -*- coding: utf-8 -*-
  import random
  import sys
  import time
  result = []
  while True:
  result.append(int(random.uniform(1,7)))
  result.append(int(random.uniform(1,7)))
  result.append(int(random.uniform(1,7)))
  print result
  count = 0
  index = 2
  pointStr = &quot;&quot;
  while index >= 0:
  currPoint = result[index]
  count += currPoint
  index -= 1
  pointStr += &quot; &quot;
  pointStr += str(currPoint)
  if count <= 11:
  sys.stdout.write(pointStr + &quot; -> &quot; + &quot;小&quot; + &quot;\n&quot;)
  time.sleep( 1 )   # 睡眠一秒
  break
  else:
  sys.stdout.write(pointStr + &quot; -> &quot; + &quot;大&quot; + &quot;\n&quot;)
  time.sleep( 1 )   # 睡眠一秒
  break
  result = []
  ===============================
  ===============================
  九九乘法表
  #!/usr/bin/python
  # -*- coding: UTF-8 -*-
  #九九乘法表
  i = 1
  while i :
  j = 1
  while j:
  print j ,&quot;*&quot;, i ,&quot; = &quot; , i * j , '  ',
  if i == j :
  break
  j += 1
  if j >= 10:
  break
  print &quot;\n&quot;
  i += 1
  if i >= 10:
  break
  ===============================
  ===============================
  去除字符串首尾的空格:
  def trim(s):
  while s[:1] == ' ':
  s = s[1:]
  while s[-1:] == ' ':
  s = s[:-1]
  return s
  str = '   Runoob     '
  print(trim(str))
  ===============================
  ===============================
  十进制转二进制
  #!/usr/bin/python
  # -*- coding: UTF-8 -*-
  denum = input(&quot;输入十进制数:&quot;)
  print denum,&quot;(10)&quot;,
  binnum = []
  # 二进制数
  while denum > 0:
  binnum.append(str(denum % 2)) # 栈压入
  denum //= 2
  print '= ',
  while len(binnum)>0:
  import sys
  sys.stdout.write(binnum.pop()) # 无空格输出print ' (2)'
  ==============================
  ==============================
  判断质数
  #!/usr/bin/python
  # -*- coding: UTF-8 -*-
  for num in range(2,100):  # 迭代 10 到 20 之间的数字
  for i in range(2,num): # 根据因子迭代
  if num%i == 0:      # 确定第一个因子
  j=num/i          # 计算第二个因子
  #         print '%d 等于 %d * %d' % (num,i,j)
  break            # 跳出当前循环
  else:                  # 循环的 else 部分
  print num, '是一个质数'
  ==============================
  =============================
  # 打印空心等边三角形
  #!/usr/bin/python
  # -*- coding: UTF-8 -*-
  rows = int(raw_input('输入行数:'))
  for i in range(0, rows):
  for k in range(0, 2 * rows - 1):
  if (i != rows - 1) and (k == rows - i - 1 or k == rows + i - 1):
  print &quot; * &quot;,
  elif i == rows - 1:
  if k % 2 == 0:
  print &quot; * &quot;,
  else:
  print &quot;   &quot;,
  else:
  print &quot;   &quot;,
  print &quot;\n&quot;
  ==============================
  冒泡排序
  #!/usr/bin/python
  # -*- coding: UTF-8 -*-
  arays = [1,5,2,7,6,15,12,9]
  for i in range(len(arays)):
  for j in range(i+1):
  if arays < arays[j]:
  # 实现连个变量的互换
  arays,arays[j] = arays[j],arays
  print arays
  ==========================
  ==========================
  打印菱形
  #!/usr/bin/python
  # -*- coding: UTF-8 -*-
  width = int(5)
  for row in range(width + 1):
  for col in range(width + 1):

  if ((abs(row ->  print &quot;*&quot;,
  else:
  print &quot; &quot;,
  print &quot; &quot;
  ====================================

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-548782-1-1.html 上篇帖子: python处理大文件的内存问题 下篇帖子: Python3.5连接Mysql-9262317
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表