#!/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 ) # 睡眠一秒
else:
sys.stdout.write(pointStr + " -> " + "大" + "\n")
time.sleep( 1 ) # 睡眠一秒
result = []
905Python While 循环语句
猜拳小游戏
#!/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/python
# -*- coding: UTF-8 -*-
import random
s = int(random.uniform(1,10))
#print(s)
m = int(input('输入整数:'))
while m != s:
if m > s:
print('大了')
m = int(input('输入整数:'))
if m < s:
print('小了')
m = int(input('输入整数:'))
if m == s:
print('OK')
break;
903Python 循环语句
八皇后问题 (循环递归法)
#* queen problem with recurison
BOARD_SIZE = 8
def under_attack(col, queens):
left = right = col
for r, c in reversed(queens):
#左右有冲突的位置的列号
left, right = left - 1, right + 1
if c in (left, col, right):
return True
return False
def solve(n):
if n == 0:
return [[]]
smaller_solutions = solve(n - 1)
return [solution+[(n,i+1)]
for i in xrange(BOARD_SIZE)
for solution in smaller_solutions
if not under_attack(i+1, solution)]
for answer in solve(BOARD_SIZE):
print answer
902Python 条件语句
if 简单条件判断一行搞定:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
a = [1,2,3]
b = a if len(a) != 0 else ""
print(b)
c=[]
d = c if len(c) != 0 else "c 是一个空列表"
print(d)
906Python While 循环语句
摇筛子游戏
905Python While 循环语句
猜拳小游戏
测试结果:
904Python While 循环语句
猜大小的游戏
903Python 循环语句
八皇后问题 (循环递归法)
902Python 条件语句
if 简单条件判断一行搞定:
输出结果为: