【MOOC嵩天Python语言程序设计】第8周 程序设计方法学

本文介绍了一个Python程序,用于模拟两个选手之间的体育竞技比赛,通过随机概率决定比赛结果,并统计胜率。此外,还展示了如何求1-100的计数和,os库的使用方法,以及自动批量安装Python第三方库的脚本。

8.1 实例13:体育竞技分析

# 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()

这个程序模拟两个选手A和B的某种竞技比赛
程序运行需要A和B的能力值(以0到1之间的小数表示)
请输入选手A的能力值(0-1): 0.5
请输入选手B的能力值(0-1): 0.6
模拟比赛的场次: 5
竞技分析开始,共模拟5场比赛
选手A获胜1场比赛,占比20.0%
选手B获胜4场比赛,占比80.0%

8.2 Python程序设计思维

求1-100的计数和

s = 0
for i in range(1,101):
    s += i
print(s)

5050

8.3 Python第三方库安装

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

8.4 模块7:os库的使用

在这里插入图片描述

import os.path as op
import time
print(
    op.abspath("bitlogo.png"),
    op.normpath("bitlogo.png"),
    op.relpath("bitlogo.png"),
    op.dirname("bitlogo.png"),
    op.basename("bitlogo.png"),
    op.join("C:/", "bitlogo.png"),
    op.exists("bitlogo.png"),
    op.isfile("bitlogo.png"),
    op.isdir("bitlogo.png"),
    op.getatime("bitlogo.png"),
    op.getmtime("bitlogo.png"),
    time.ctime(op.getctime("bitlogo.png")),
    op.getsize("bitlogo.png"),
    sep='\n'
    )

 C:\Users\Nah\Desktop\bitlogo.png
bitlogo.png
bitlogo.png

bitlogo.png
C:/bitlogo.png
True
True
False
1594442461.3920164
1594442461.3920164
Sat Jul 11 12:15:36 2020
349012
import os
os.system("C:\\Windows\\System32\\calc.exe")

在这里插入图片描述

import os
os.system("C:\\Windows\\System32\\mspaint.exe C:\\Users\\Nah\\Desktop\\bitlogo.png")

在这里插入图片描述

import os
print(
    os.chdir("D:"),
    os.getcwd(),
    os.getlogin(),
    os.cpu_count(),
    os.urandom(3),
    sep='\n'
)
None
D:\
Nah
8
b'=\xe9\xfb'

8.5 实例14:第三方库自动安装脚本

在这里插入图片描述

# BatchInstall.py
import os
libs = {"numpy", "matplotlib", "pillow", "sklearn", "requests", \
        "jieba", "beautifulsoup4", "wheel", "networkx", "sympy", \
        "pyinstaller", "django", "flask", "werobot", "pyqt5", \
        "pandas", "pyopengl", "pypdf2", "docopt", "pygame"}
try:
    for lib in libs:
        os.system("pip3 install " + lib)
    print("Successful")
except:
    print("Failed Somehow")
/*********************************************************** 实验名称:可调电子钟(数码管显示)示例程序 程序说明:按键S1切换时分秒,按下S6加一,按下S3减一 ***********************************************************/ #include unsigned char code SEG7[11]={0xC0,/*0*/ 0xF9,/*1*/ 0xA4,/*2*/ 0xB0,/*3*/ 0x99,/*4*/ 0x92,/*5*/ 0x82,/*6*/ 0xF8,/*7*/ 0x80,/*8*/ 0x90,/*9*/ 0xbf,/*-*/ }; unsigned short int tcount; struct time { unsigned char second; unsigned char minute; unsigned char hour; }time1; /***************************************/ /* 延时子程序 */ /***************************************/ void delay(k) unsigned int k; { unsigned char j; while((k--)!=0) for(j=0;j<125;j++); } /***************************************/ /* 时钟调整子程序 */ /***************************************/ TimeUpDown(n,x,y) unsigned char n,x,y; { if(P3_2==0) { delay(50); if(P3_2==0) { n++; if(n==x) n=0; while(P3_2==0); } } if(P3_3==0) { delay(50); if(P3_3==0) { if(n==0) n=y; else n--; while(P3_3==0); } } return n; } /***************************************/ /* 显示子程序 */ /***************************************/ void display(DisSecondH,DisSecondL,DisMinutH,DisMinutL,DisHourH,DisHourL) unsigned char DisSecondH,DisSecondL,DisMinutH,DisMinutL,DisHourH,DisHourL; { P0=SEG7[DisSecondH]; //显示秒钟的十位 P2=0xfe; delay(2); P0=SEG7[DisSecondL]; //显示分钟的个位 P2=0xfd; delay(2); P0=0xbf; //显示- P2=0xfb; delay(2); P0=SEG7[DisMinutH]; //显示秒钟的十位 P2=0xf7; delay(2); P0=SEG7[DisMinutL]; //显示分钟的个位 P2=0xef; delay(2); P0=0xbf; //显示- P2=0xdf; delay(2); P0=SEG7[DisHourH]; //显示小时的个位 P2=0xbf; delay(2); P0=SEG7[Dis
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值