How to write a trisomy climate modeling program with Python

First statement about the so-called three-body climate modeling program is very simple, not really 3D effect or mathematical models and the like, is just a textual representation of the simulation program. Some parts of the program may be less stringent, so please bear with me also.

The so-called three-body climate modeling, is the case of the sun appears to classify the discussion, then it will show up. For example, the sun is a constant era, two sun may be on the 2nd or Pente volley on the 2nd, three sun may also be three days or three days Pente volley. Can understand this point, the three-body climate modeling program is very good writing.

Before writing program, you must first import a library. Because of the complexity of the three-body problem, let's put the probability of three one-third of the sun appeared positioning, that is to use a random method. So here we need to import random library randint-- random number function.

from random import randint

After insertion of the random library, first determine several variables. As the three bodies in the world have three sun, and may appear in a different position, so let's define variables:

# Where 0 represents the sun of Meteor, represents the sun appeared 
Sun1 the randint = (0, 1 ) 
Sun2 = the randint (0, 1 ) 
sun3 = the randint (0, 1 )
 # 1 ~. 3 represent different positions, if is the same position Lianzhu 
sun1_pos the randint = (. 1,. 3 ) 
sun2_pos = the randint (. 1,. 3 ) 
sun3_pos = the randint (. 1,. 3)

In addition to these basic variables, we also need to output two climate types and eras types of variables: weather and era_mode

weather = ""
era_mode = ""

Because behind these two variables output in text form, it is a String

After completing the variable is set, it's time to consider climatic conditions of the three-body world.

In accordance with the description of the book, we can be divided into (Heng era is not discussed): three meteor (ie no sun), the 2nd volley, 2nd remarks, volley three days and three days Pente

This way, you can start to write a program.

First, there is no sun, namely meteor three situations:

IF Sun1 Sun2 == == == 0 sun3:    # three sun did not appear 
    weather Weather = " three meteor " 
    era_mode = " chaotic era " 
    Print (era_mode, weather Weather)     # Output climatic conditions

Then detect whether the era of constant, namely a sun:

if sun1 == 1 and sun2 == sun3 == 0:
    era_mode = "恒纪元"
    print(era_mode)
elif sun2 == 1 and sun1 == sun3 == 0:
    era_mode = "恒纪元"
    print(era_mode)
elif sun3 == 1 and sun1 == sun2 == 0:
    era_mode = "恒纪元"
    print(era_mode)

Then three of the sun is the case:

IF Sun1 sun3 == == == Sun2 1 :
     IF sun1_pos sun2_pos == == sun3_pos: 
        weather Weather = " three days Pente " 
        era_mode = " chaotic era " 
        Print (era_mode, weather Weather)
     the else : 
        weather Weather = " three days volley " 
        era_mode = " chaotic era " 
        Print (era_mode, weather Weather)

Finally, the case of two of the sun, it is relatively trouble:

IF Sun1 == == Sun2. 1 :
     IF sun1_pos == sun2_pos: 
        Weather = " the 2nd Lianzhu " 
        era_mode = " chaos era " 
        Print (era_mode, Weather)
     the else : 
        Weather = " the 2nd volley " 
        era_mode = " chaos era " 
        Print (era_mode, weather Weather)
 elif Sun1 sun3 == == 1 :
     IF sun1_pos == sun2_pos: 
        weather Weather = " 2nd Pente "
        era_mode = " chaos era " 
        Print (era_mode, Weather)
     the else : 
        Weather = " 2nd volley " 
        era_mode = " chaos era " 
        Print (era_mode, Weather)
 elif Sun2 == == sun3. 1 :
     IF sun2_pos == sun3_pos: 
        Weather = " 2nd Pente " 
        era_mode = " chaotic era " 
        Print (era_mode, weather Weather)
     the else : 
        weather Weather =" 2nd volley " 
        era_mode = " chaotic era " 
        Print (era_mode, weather Weather)

Note that this meteor from three, one sun, three sun, the sun is not the order of two upset, otherwise not allowed to judge the climate situation occurs because this program is to run away from the top down It is linear. For example, if there are three sun, the sun and two of three judges before the sun's judgment, the situation is only output on the 2nd or 2nd volley Pente will appear when the program is running, but not output volley or three days Pente three days.

Of course, you can also make use of other methods to determine the sort of climate change, for example, you can determine what all these different functions, wrote in making this judgment, but here is not to be repeated here.

Finally impress all the code:

1  from Random Import the randint
 2  
. 3  # 0 being the sun of Meteor, represents the sun appeared 
. 4 Sun1 the randint = (0, 1 )
 . 5 Sun2 the randint = (0, 1 )
 . 6 sun3 the randint = (0, 1 )
 . 7  # 1-3 represent different positions, if the same position is Lianzhu 
. 8 sun1_pos = the randint (1, 3 )
 . 9 sun2_pos = the randint (1, 3 )
 10 sun3_pos = the randint (1, 3 )
 . 11  
12 is Weather = "" 
13 is era_mode = "" 
14  
15  IF Sun1 Sun2 == == == 0 sun3:    #The sun did not appear three 
16      weather Weather = " three meteor " 
17      era_mode = " chaotic era " 
18      Print (era_mode, weather Weather)     # Output climatic conditions 
19  elif Sun1 1 == and Sun2 sun3 == == 0:
 20      era_mode = " constant epoch " 
21 is      Print (era_mode)
 22 is  elif Sun2 ==. 1 and Sun1 sun3 == == 0:
 23 is      era_mode = " constant epoch " 
24      Print (era_mode)
 25  elif==. 1 sun3 and Sun1 Sun2 == == 0:
 26 is      era_mode = " constant epoch " 
27      Print (era_mode)
 28  elif Sun1 sun3 == == == Sun2. 1 :
 29      IF sun1_pos sun2_pos == == sun3_pos:
 30          Weather = " three days Lianzhu " 
31 is          era_mode = " arbitrary epoch " 
32          Print (era_mode, Weather)
 33 is      the else :
 34 is          Weather = " three days volley " 
35          era_mode = "Arbitrary epoch " 
36          Print (era_mode, Weather)
 37 [  elif Sun1 == == Sun2. 1 :
 38 is      IF sun1_pos == sun2_pos:
 39          Weather = " 2nd Lianzhu " 
40          era_mode = " arbitrary epoch " 
41 is          Print (era_mode, Weather)
 42 is      the else :
 43 is          Weather = " 2nd volley " 
44 is          era_mode = " arbitrary epoch " 
45          Print (era_mode, Weather)
 46 is  elif== == sun3. 1 Sun1 :
 47      IF sun1_pos == sun2_pos:
 48          Weather = " 2nd Lianzhu " 
49          era_mode = " arbitrary epoch " 
50          Print (era_mode, Weather)
 51 is      the else :
 52 is          Weather = " 2nd volley " 
53 is          era_mode = " arbitrary epoch " 
54 is          Print (era_mode, Weather)
 55  elif Sun2 == == sun3. 1 :
 56 is      IF sun2_pos == sun3_pos:
 57 is         = Weather " 2nd Lianzhu " 
58          era_mode = " arbitrary epoch " 
59          Print (era_mode, Weather)
 60      the else :
 61 is          Weather = " 2nd volley " 
62 is          era_mode = " arbitrary epoch " 
63 is          Print (era_mode, Weather)

Total number of lines does not exceed 100 lines, or very streamlined

If you want to view the generated sun, may be added after completion of variable (i.e., at 11 lines of code above) was added:

print("sun1: %u , sun2: %u , sun3: %u" % (sun1, sun2, sun3))
print("sun1_pos: %u , sun2_pos: %u , sun3: %u" % (sun1_pos, sun2_pos, sun3_pos))

Hey hey? You asked me to print, enter% What does this mean? python3 grammar small mind can help you, after all, this syntax is very important, it can be removed from a comma brought in a grid space in print.

2020/3/3

 

Guess you like

Origin www.cnblogs.com/PekingMan/p/ThreeBodyWeatherMaker.html