⏰ preface
Imperceptibly today has passed half, this thought yesterday can update the article today at this time began to code words. Today concludes two projects: guess numbers and predict probabilities. Programming can make people understand some truth, even let you think that seemingly unrelated random numbers can be predicted, so let’s play with these two groups of code!
😝 guess Numbers
Let’s say Joe is 18 years old. When she is asked her age, she doesn’t want to give a direct answer, but she is polite enough to tell her age. So Joe wants to write a program that lets people guess by giving hints. Here we use the while statement to construct such a program.
joe_age = 18 guess = int(input(">>:")) while guess ! = if guess > joe_age: print(" print ") Elif guess < joe_age: print(" ) guess = int(input(">>:") print(" )Copy the code
The while loop differs from the for loop in that it can loop indefinitely. We don’t know how many guesses we have to make, so the while loop is more suitable for this scenario.
guess = int(input(">>:"))Copy the code
If and elif conditional statements are also used:
If guess > joe_age: print(" Guess too big, try smaller... Elif guess < joe_age: print(" ) guess = int(input(">>:") print(" )Copy the code
When guessing your age is greater than your actual age, print “Too big, try smaller…” “, when the guess is less than the actual age, print “guess too small, try older…” When guessing age equals actual age, print “Congratulations, you guessed right!” The running results of the program are as follows:
🏆 Win probability prediction
The scenario is as follows: 1. In the badminton match between Little A and Little B, Little A has A 55% probability of winning each ball, while little B has A 45% probability of winning each ball; 2. In each game, the first one to score 21 points wins; 3. Given a hypothetical game of 10,000 games, how many would each player win? First, construct the top-level framework:
def main(): Outputs prob_A, prob_B, number_of_games = get inputs() win_A, win_B = sim_n_games(prob_A, prob_B, Print_summary (win_A, win_B, number_of_games) # summary outputCopy the code
Def main() defines the functions main, prob_A small A win probability per shot,prob_B small B win probability per shot,number_of_games Number of matches played. Based on the above parameters, the number of wins win_A, win_B can be obtained. Finally, the summary is printed print_summary.
Input raw data
def get_inputs(): Eval (input(" please input player A's probability of winning per shot (0~1):")) prob_B = round(1-prob_a, 2) #2 player match 1-prob_A = prob_B number_of_games = eval(input(" Please enter number of simulated matches (positive integer) : ") print(" Number of simulated matches: ", number_of_games) print(" small A win probability per ball: ", prob_A) print(" Probability of winning per ball: ", prob_B) return prob_A, prob_B, number_of_games # number_of_games = get_inputs() print(prob_A, prob_B, number_of_games)Copy the code
Multiple game simulations
Play multiple game simulation (score_A, score_B is to get the score and one of them reaches 21 loop stop:
def sim_n_games(prob_A, prob_B, number_of_games): Win_A, win_B = 0, 0 # initialize the number of games won by A and B for I in range(number_of_games): Score_A, score_B = sim_one_game(prob_A, prob_B) # score_A > score_B win_A +=1 else: win_B +=1 return win_A, win_BCopy the code
Single game simulation
Then we simplified it again, taking multiple races and reducing them to a simulated race, and doing it 10,000 times:
import random def sim_one_game(prob_A, prob_B): While not game_over(score_A, score_B): score_A +=1 else : score_B +=1 return score_A, score_BCopy the code
Note that if random.random() < prob_A: the random data is less than 55% of the probability that little A wins each ball, then A wins once.
The summary output
def print_summary(win_A, win_B, number_of_games): Format (number_of_games) # print(" player A wins {0} matches, Format (win_A, win_A/number_of_games) # print(" player B wins {0} matches, % {1:.1%}". Format (win_B, win_B/number_of_games)) #B The probability of winning divided by the total number of games multiplied by 100%Copy the code
Final program
import random def get_inputs(): Eval (input(" please input player A's probability of winning per shot (0~1):")) prob_B = round(1-prob_a, 2) #2 player match 1-prob_A = prob_B number_of_games = eval(input(" Please enter number of simulated matches (positive integer) : ") print(" Number of simulated matches: ", number_of_games) print(" small A win probability per ball: ", prob_A) print(" Probability of winning per ball: ", prob_B) return prob_A, prob_B, number_of_games # number_of_games = get_inputs() #print(prob_A, prob_B, number_of_games) def game_over(score_A, score_B): Score_A == 21 or score_B == 21 def sim_one_game(prob_A, prob_B) Score_A, score_B = 0,0 while not game_over(score_A, score_B): if random. Random () < prob_A: score_A += 1 else : score_B += 1 return score_A, score_B def sim_n_games(prob_A, prob_B, number_of_games): Win_A, win_B = 0, 0 # initialize the number of games won by A and B for I in range(number_of_games): Score_A, score_B = sim_one_game(prob_A, prob_B) # score_A > score_B win_A += 1 else: win_B += 1 return win_A, win_B def print_summary(win_A, win_B, number_of_games): Format (number_of_games) # print(" player A wins {0} matches, Format (win_A, win_A/number_of_games) # print(" player B wins {0} matches, {1:.1%}". Format (win_B, win_B/number_of_games)) # def main(): Prob_A, prob_B, number_of_games = get_inputs() Print_summary (win_A, win_B, number_of_games) # if __name__ == "__main__": main()Copy the code
To this point, the entire program is finished, the code in the comments need to read carefully.
👍 summary
Two small cases of practice, let me feel that every simple automation implementation is behind countless bitterness. After the great division of labor in society, people’s professional is more and more detailed and more and more specialized. The general public uses machines carelessly to automate and work efficiently. However, the more efficient, convenient and stupid intelligence, the more someone pays time and energy to help you complete more things to be considered. Long live the geek! In addition, in the probability analysis of this case, the probability of winning or losing A single ball is almost the same as that of B, but there is A surprising difference after many matches, just as the so-called “A miss is as good as A mile!”
See you in the next chapter! 🙋 ♀ ️
Reproduced without communication, will be investigated for legal responsibility, please respect the original fruits of labor!