所向无敌 发表于 2018-8-9 06:14:34

python基础小程序

  ========================================
  猜大小的游戏
  #!/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
  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
  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 =
  for i in range(len(arays)):
  for j in range(i+1):
  if arays < arays:
  # 实现连个变量的互换
  arays,arays = arays,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]
查看完整版本: python基础小程序