Python Learning Notes (8) - Programming Design Methodology
1. Effective solutions to complex problems
1. Top-down:
2. From bottom to top:
2. Computing Thought and Programming
1. Computational thinking:
1.1 Logical thinking:
1.2 Positive thinking:
1.3 Computing thinking:
1.4 Differences among Three Thoughts
Examples:
eg1:
eg2:
eg3:
eg4:
eg5:
1.5 Computing Thinking and Programming
3. Computing Ecology and Python Language
1. Computational Ecology:
2. Computing Ecology and Python Language
3. Calculating the Value of Ecology
IV. User Experience and Software Products
1. Implement Functions - > Focus on Experience
2. Ways to improve user experience
2.1 Progress Show
2.2 Exception handling
2.3 Other methods
5. Basic Programming Modes
1. Introduction to IPO:
2. Steps:
3. Basic design patterns:
3.1 Top-down design:
3.2 Modular design:
3.3 Configurable design:
4. Steps for application development: from application requirements to software products
6. Examples: Sports Competition
1. Overall Program Framework and Steps
2. Draw the overall framework
Stage 1: Functions:
2.1 main() function
def main(): printIntro() probA, probB, n = getInputs() winsA, winsB = simNGames(n, probA, probB) printSummary(winsA, winsB)
2.2 printInfo() function: introductory content to improve user experience
def printIntro(): print("This program simulates two players A and B Of some kind of competition") print("Program Running Requirements A and B Ability value (expressed as a decimal number between 0 and 1)")
2.3 getInputs() function
def getInputs(): a = eval(input("Please enter a player A Ability Value(0-1): ")) b = eval(input("Please enter a player B Ability Value(0-1): ")) n = eval(input("Number of simulated matches: ")) return a, b, n
2.4 printSummary() function)
def printSummary(winsA, winsB): n = winsA + winsB print("Athletic analysis started, total simulation{}Game".format(n)) print("Player A Win victory{}Game, proportions{:0.1%}".format(winsA, winsA/n)) print("Player B Win victory{}Game, proportions{:0.1%}".format(winsB, winsB/n))
Stage 2: Simulate N Games
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
Stage 3: Judging the end of the board by score
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
Complete code:
#MatchAnalysis.py from random import random def printIntro(): print("This program simulates two players A and B Of some kind of competition") print("Program Running Requirements A and B Ability value (expressed as a decimal number between 0 and 1)") def getInputs(): a = eval(input("Please enter a player A Ability Value(0-1): ")) b = eval(input("Please enter a player B Ability Value(0-1): ")) n = eval(input("Number of simulated matches: ")) 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("Athletic analysis started, total simulation{}Game".format(n)) print("Player A Win victory{}Game, proportions{:0.1%}".format(winsA, winsA/n)) print("Player B Win victory{}Game, proportions{:0.1%}".format(winsB, winsB/n)) def main(): printIntro() probA, probB, n = getInputs() winsA, winsB = simNGames(n, probA, probB) printSummary(winsA, winsB) main()
Run result: