Chinaunix首页 | 论坛 | 博客
  • 博客访问: 147381
  • 博文数量: 61
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 590
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-03 15:26
文章分类

全部博文(61)

文章存档

2010年(9)

2009年(52)

我的朋友

分类: Python/Ruby

2009-03-25 13:25:31

python编程金典--读书笔记,in Py3k
第二章
3/4 = 0.75
3//4 = 0
运算符/在3.0中做ture除法

Py3k不再使用不等号<>用!=

for语句结构格式:
for element in sequence:
    statement(s)


################################################
# 使用radom函数,做的双骰子游戏
################################################
import random

def rollNum1():
    num1 = random.randrange(1,7)
    num2 = random.randrange(1,7)
    print('rollNum = ',num1,'+',num2, '=',num1+num2)
    return num1+num2

def rollNum2():
    num1 = random.randrange(1,7)
    num2 = random.randrange(1,7)
    print('___rollNum = ',num1,'+',num2, '=',num1+num2)
    return num1+num2

print('----------start---------------')
winCounter = 0
loseCounter = 0

for num in range(1,10001,1):
    stat = 'continue'
    flag = rollNum()
    if(7 ==flag or 11 == flag):
        winCounter +=1
        stat = 'win'
    elif(2==flag or 3==flag or 12==flag):
        loseCounter +=1
        stat = 'lose'
    else:
        tempf = rollNum2()
        while('continue'==stat):
            if(7 == tempf):
                loseCounter += 1
                stat = 'lose'
                
            elif(flag == tempf):
                winCounter += 1
                stat = 'win'
            else:
                tempf = rollNum2()
                

print('win=',winCounter, '\nlose=' ,loseCounter)
            



##################################
# 斐波那契实验,有种放到5000~
###########################

def feibonaqi(num=0):
    if((0==num) or (1==num)):
        return num
    else:
        print("feibonaqi num now=",num)
        return feibonaqi(num-1)+feibonaqi(num-2)

test = int(input("put a num="))

print("haha",feibonaqi(test))



作品loseblue创作,采用进行许可。
阅读(893) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~