Erlo

Python学习笔记(八)

2019-11-07 17:00:29 发布   361 浏览  
页面报错/反馈
收藏 点赞

Python学习笔记(八)—— 程序设计方法学

一、解决复杂问题的有效方法

  1、自顶向下:

  

  2、自底向上:

  

 

二、计算思维与程序设计

  1、计算思维:

    1.1 逻辑思维:

    1.2 实证思维:

    1.3 计算思维:

      

 

 

     1.4 三种思维的区别

      举例说明:

      eg1:

      

 

      eg2:

      

 

      eg3:

      

 

      eg4:

      

 

      eg5:

      

 

      1.5 计算思维与程序设计

      

 

 

 

三、计算生态与Python语言

  1、计算生态:

   

  

 

  2、计算生态与Python语言

    

    

 

  3、计算生态的价值

  

 

 

四、用户体验与软件产品

  1、实现功能 -> 关注体验

    

 

  2、提高用户体验的方法

    2.1 进度展示

      

 

    2.2 异常处理

      

 

    2.3 其他方法

      

 

 

五、基本的程序设计模式

  1、IPO介绍:

    

 

  2、步骤:

    

 

  3、基本的设计模式:

    3.1 自顶向下设计:

   

 

    3.2 模块化设计:

    

    

 

   3.3 配置化设计:

   

 

  4、应用开发的步骤:从应用需求到软件产品

    

    

 

六、实例:体育竞技 

  1、程序总体框架及步骤

    

  2、画出总体框架

    

 

    第一阶段:函数:

    2.1 main()函数

def main():
    printIntro()
    probA, probB, n = getInputs()
    winsA, winsB = simNGames(n, probA, probB)
    printSummary(winsA, winsB)

     2.2  printInfo()函数:介绍性内容,提高用户体验

def printIntro():
    print("这个程序模拟两个选手A和B的某种竞技比赛")
    print("程序运行需要A和B的能力值(以0到1之间的小数表示)")

     2.3  getInputs()函数

def getInputs():
    a = eval(input("请输入选手A的能力值(0-1): "))
    b = eval(input("请输入选手B的能力值(0-1): "))
    n = eval(input("模拟比赛的场次: "))
    return a, b, n

    2.4 printSummary()函数    

def printSummary(winsA, winsB):
    n = winsA + winsB
    print("竞技分析开始,共模拟{}场比赛".format(n))
    print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n))
    print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n))

  第二阶段:模拟N局比赛

 

def simNGames(n, probA, probB):
    winsA, winsB = 0, 0
    for i in range(n):
        scoreA, scoreB = simOneGame(probA, probB)
        if scoreA > scoreB:
            winsA += 1
        else:
            winsB += 1
    return winsA, winsB

  第三阶段:根据分数判断局的结束

  

def simOneGame(probA, probB):
    scoreA, scoreB = 0, 0
    serving = "A"
    while not gameOver(scoreA, scoreB):
        if serving == "A":
            if random() < probA:
                scoreA += 1
            else:
                serving="B"
        else:
            if random() < probB:
                scoreB += 1
            else:
                serving="A"
    return scoreA, scoreB

 

 完整代码:

#MatchAnalysis.py
from random import random
def printIntro():
    print("这个程序模拟两个选手A和B的某种竞技比赛")
    print("程序运行需要A和B的能力值(以0到1之间的小数表示)")
def getInputs():
    a = eval(input("请输入选手A的能力值(0-1): "))
    b = eval(input("请输入选手B的能力值(0-1): "))
    n = eval(input("模拟比赛的场次: "))
    return a, b, n
def simNGames(n, probA, probB):
    winsA, winsB = 0, 0
    for i in range(n):
        scoreA, scoreB = simOneGame(probA, probB)
        if scoreA > scoreB:
            winsA += 1
        else:
            winsB += 1
    return winsA, winsB
def gameOver(a,b):
    return a==15 or b==15
def simOneGame(probA, probB):
    scoreA, scoreB = 0, 0
    serving = "A"
    while not gameOver(scoreA, scoreB):
        if serving == "A":
            if random() < probA:
                scoreA += 1
            else:
                serving="B"
        else:
            if random() < probB:
                scoreB += 1
            else:
                serving="A"
    return scoreA, scoreB
def printSummary(winsA, winsB):
    n = winsA + winsB
    print("竞技分析开始,共模拟{}场比赛".format(n))
    print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n))
    print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n))
def main():
    printIntro()
    probA, probB, n = getInputs()
    winsA, winsB = simNGames(n, probA, probB)
    printSummary(winsA, winsB)
main()

 

 运行结果: 

 

 

 

 

 

 

 

 

 

 

 

 

  

 

  

 

 

 

 

登录查看全部

参与评论

评论留言

还没有评论留言,赶紧来抢楼吧~~

手机查看

返回顶部

给这篇文章打个标签吧~

棒极了 糟糕透顶 好文章 PHP JAVA JS 小程序 Python SEO MySql 确认