========================================
猜大小的游戏
#!/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,"次总算猜对了,恭喜恭喜"
break;
===============================
===============================
猜拳小游戏
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import random
while 1:
s = int(random.randint(1, 3))
if s == 1:
ind = "石头"
elif s == 2:
ind = "剪子"
elif s == 3:
ind = "布"
m = raw_input('输入 石头、剪子、布,输入"end"结束游戏:')
blist = ['石头', "剪子", "布"]
if (m not in blist) and (m != 'end'):
print "输入错误,请重新输入!"
elif (m not in blist) and (m == 'end'):
print "\n游戏退出中..."
break
elif m == ind :
print "电脑出了: " + ind + ",平局!"
elif (m == '石头' and ind =='剪子') or (m == '剪子' and ind =='布') or (m == '布' and ind =='石头'):
print "电脑出了: " + ind +",你赢了!"
elif (m == '石头' and ind =='布') or (m == '剪子' and ind =='石头') or (m == '布' and ind =='剪子'):
print "电脑出了: " + ind +",你输了!"
===============================
===============================
摇筛子游戏
#!/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 = ""
while index >= 0:
currPoint = result[index]
count += currPoint
index -= 1
pointStr += " "
pointStr += str(currPoint)
if count <= 11:
sys.stdout.write(pointStr + " -> " + "小" + "\n")
time.sleep( 1 ) # 睡眠一秒
break
else:
sys.stdout.write(pointStr + " -> " + "大" + "\n")
time.sleep( 1 ) # 睡眠一秒
break
result = []
===============================
===============================
九九乘法表
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#九九乘法表
i = 1
while i :
j = 1
while j:
print j ,"*", i ," = " , i * j , ' ',
if i == j :
break
j += 1
if j >= 10:
break
print "\n"
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("输入十进制数:")
print denum,"(10)",
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 " * ",
elif i == rows - 1:
if k % 2 == 0:
print " * ",
else:
print " ",
else:
print " ",
print "\n"
==============================
冒泡排序
#!/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[i] < arays[j]:
# 实现连个变量的互换
arays[i],arays[j] = arays[j],arays[i]
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 - width/2) + abs(col - width/2)) == width/2):
print "*",
else:
print " ",
print " "
====================================