1. Top down (analysis), bottom up (execution)
-A general problem is expressed in the form of several small problems. The same method is used to further decompose the small problems until they can be solved simply and clearly by computer
-Unit test and gradual assembly. Follow the top-down path. Until now, all parts of the system have been tested and verified with the idea of assembly
2. Sports analysis
A & B, three wins in five games
Step: print the introductory information of the program, and obtain the operation parameters of the program: proA,proB,n. use the ability values of players A and B to simulate the n game, and output the matches and probability of players A and B winning the game
from random import random def printIntro(): print("This program simulates two players A and B Some kind of competitive game") print("Required for program operation A and B Capability value of (expressed as a decimal between 0 and 1)") def getInputs(): a = eval(input("Please enter players A Capability value of (0-1): ")) b = eval(input("Please enter players B Capability value of (0-1): ")) n = eval(input("Number of simulated matches:")) return a,b,n def printSummary(winsA,winsB): n = winsA + winsB print("Start of competitive analysis, total simulation{}Match".format(n)) print("Player A Win victory{}Games, percentage{: 0.1%}".format(winsA,winsA/n)) print("Player B Win victory{}Games, percentage{: 0.1%}".format(winsB,winsB/n)) 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 simNGames(n,proA,proB): winsA,winsB = 0,0 for i in range(n): scoreA,scoreB = simOneGame(proA,proB) if scoreA > scoreB: winsA += 1 else: winsB += 1 return winsA,winsB def main(): printIntro() probA,probB,n=getInputs() winsA,winsB = simNGames(n,probA,probB) printSummary(winsA,winsB) main()
3, os Library
os library provides general and basic operating system interaction functions
-os library is a standard python library, including hundreds of functions, commonly used: path operation, process management, environment parameters
Path operation: the os.path sub library takes path as the entry, which is used to operate and process the file path
import os.path,time #import os.path as op #os.path.abspath(path) returns the absolute path of path in the current system print(os.path.abspath("New era.txt")) #The representation of normalized path is divided by \ \ in a unified way, but the path is written in a unified way and cannot be obtained print(os.path.normpath("D:/Python37/cources\Internship 1.pdf")) #Returns the relative path between the current program and the file print(os.path.relpath("C://Users//lenovo//Desktop / / new era. txt","C: ") (the starting point of drive D is not good, why! print(os.path.relpath("D:\Python37\cources\01day\pywcloud.png")) #Disk D is OK #os.path.dirname(path) returns the directory name in path print(os.path.dirname("D:\Python37\cources\01day\pywcloud.png")) #Returns the last file name in the path print(os.path.basename("C://Users//lenovo//Desktop / / new era. txt ")) #os.path.join(path,*paths) combines path and paths to return a path string print(os.path.join("D:/","PYE/file.txt")) #os.path.exists(path) judges whether the file or directory corresponding to path exists, and returns True or False print(os.path.exists("D://pye//file.txt")) #os.path.isfile(path) determines whether the path corresponds to an existing file, and returns True or False print(os.path.isfile("D://pye//file.txt")) #Judge whether the path corresponds to an existing directory, and return True or False print(os.path.isdir("D://pye//file.txt")) #Return the last access time of the file or directory corresponding to the path print(os.path.getatime("D:\Python37\cources\01day\pywcloud.png")) #Return the last modification time of the file or directory corresponding to path print(os.path.getmtime("D:\Python37\cources\01day\pywcloud.png")) #Return the creation time of the file or directory corresponding to path print(time.ctime(os.path.getctime("D:\Python37\cources\01day\pywcloud.png"))) #Returns the size of the file corresponding to path, in bytes print(os.path.getsize("D:\Python37\cources\01day\pywcloud.png"))
Process management: start other programs in the system os.system(command)
-Execute a program or command command. In windows system, the call return information with the return value of cmd is returned
import os os.system("C:\\Windows\\System64\\calc.exe") os.system("Running software" "picture")
Environment parameters: obtain system software and hardware information and other environment parameters
#Modify the path of the current program operation >>> os.chdir("D:") #Returns the current path of the program >>> os.getcwd() 'D:\\Python37' #Get current system login user name >>> os.getlogin() 'lenovo' #Get the number of CPU s in the current system >>> os.cpu_count() 4 #Obtain n-byte random strings, which are usually used for encryption and decryption operations. Some strings cannot be displayed normally >>> os.urandom(10) b':NYN\xea}\x9d\x7f\xca%' >>> os.urandom(12) b'\xf3\x95\xa9\xfdZV\xfc&\xb6\xf4X\xae'