Python Using random Module to Realize Dice Game Sample Code

  • 2021-11-02 01:46:32
  • OfStack

Introduced content

According to "Modern Design Method of Python Programming" published by People's Post and Telecommunications Press, the seventh question in P102 exercise-dice game, the code is compiled.

Topic requirements

In a game, two people roll dice five times in turn, and accumulate the points thrown each time. After five games, the one with the larger accumulated points wins, while the other with the same points is a draw. According to this rule, the dice game is realized, and the WINNER after 50 sets is calculated (the winner who wins the most in 50 sets is the final WINNER).

Examination: There are 50 games in total. There are 5 games in a game, and each side rolls dice once in each game. After the end of 5 games, the score is counted, and the one with the highest score wins. At this point, one game is over. After 50 games, the one who wins the most games is the final WINNER.

Writing Start

Start the wonderful journey of Python! ↓ ↓ ↓

Import of modules and definition of initial variables

Unification 1 declares variables, which is convenient for subsequent management and use.


import random  # Import module 
#  ↓ ↓ ↓   Define initial variables 
a =0  #  Used to record characters A The result of throwing 
b =0  #  Used to record characters B The result of throwing 
level_score =0  #  Used to record the number of draws between the two sides 
score_A =0  #  Character A Number of victories per small game 
score_B =0  #  Character B Number of victories per small game 

Start the competition process

We use for … in range (50) to simulate 50 games. The new for … in … range (5) is nested in the loop as a simulation of 5 processes in each game


for x in range(50):  #  Go on 50 Plate match 
    for n in range(5):  #  Simulate each shake of both sides 5 Secondary dice 
        person_A =random.randint(1,6)  #  Use random Modular randit Go on 1~6 Simulate the process of rolling dice 
        a +=person_A  #  Save the throwing result 

        person_B =random.randint(1,6)#  Use random Modular randit Go on 1~6 Simulate the process of rolling dice 
        b +=person_B  #  Save the throwing result 
    if a>b:  #  Compare the total throwing results of both sides, and save the comparison results to the final result of this game 
        score_A+=1
    elif a<b:
        score_B+=1
    else:
        level_score+=1
    a =0  #  Reset the scores of both sides and enter the following 1 Wheel cycle 
    b =0  #  Reset the scores of both sides and enter the following 1 Wheel cycle 
    #  If you don't reset the score here, the result of this throw will enter the following 1 Sub-cycle, contrary to the requirements of the topic and the fairness of the competition. 

Statistical results after the game

We have simulated each game before, and now we have to write the code required at the end of the topic.
Because we used score_A and score_B as variables to record the end of each game, we only need to compare the sizes of the previous variables here.


if score_A>score_B:  #  Compare the final results and the game is over 
    print(f' This competition A Win, A Total division is: {score_A} , B Total division is: {score_B}, The number of draws during the competition is: {level_score}')
elif score_A<score_B:
    print(f' This competition B Win ,A Total division is: {score_A},B Total division is: {score_B}, The number of draws during the competition is: {level_score}')
else:
    print(f' This competition is a draw, and the total score of both sides is: A{score_A} vs B{score_B}, The number of draws during the competition is: {level_score}')

Complete code


import random  # Import module 
#  ↓ ↓ ↓   Define initial variables 
a =0  #  Used to record characters A The result of throwing 
b =0  #  Used to record characters B The result of throwing 
level_score =0  #  Used to record the number of draws between the two sides 
score_A =0  #  Character A Number of victories per small game 
score_B =0  #  Character B Number of victories per small game 

for x in range(50):  #  Go on 50 Plate match 
    for n in range(5):  #  Simulate each shake of both sides 5 Secondary dice 
        person_A =random.randint(1,6)  #  Use random Modular randit Go on 1~6 Simulate the process of rolling dice 
        a +=person_A  #  Save the throwing result 

        person_B =random.randint(1,6)#  Use random Modular randit Go on 1~6 Simulate the process of rolling dice 
        b +=person_B  #  Save the throwing result 
    if a>b:  #  Comparison 5 After the game, the total throwing results of both sides will be saved to the final result of this game 
        score_A+=1 # A Win this set 
    elif a<b: # B Win this set 
        score_B+=1
    else:
        level_score+=1
    a =0  #  Reset the scores of both sides and enter the following 1 Wheel cycle 
    b =0  #  Reset the scores of both sides and enter the following 1 Wheel cycle 
    #  If you don't reset the score here, the result of this throw will enter the following 1 Sub-cycle, contrary to the requirements of the topic and the fairness of the competition. 

if score_A>score_B:  #  Compare the final results and the game is over 
    print(f' This competition A Win, A Total division is: {score_A} , B Total division is: {score_B}, The number of draws during the competition is: {level_score}')
elif score_A<score_B:
    print(f' This competition B Win ,A Total division is: {score_A},B Total division is: {score_B}, The number of draws during the competition is: {level_score}')
else:
    print(f' This competition is a draw, and the total score of both sides is: A{score_A} vs B{score_B}, The number of draws during the competition is: {level_score}')


Related articles: