用python模拟三体运动_怎么用Python写一个三体的气候模拟程序
首先聲明一下,這個所謂的三體氣候模擬程序還是很簡單的,沒有真的3D效果或數學模型之類的,只不過是一個文字表示的模擬程序。該程序的某些地方可能不太嚴謹,所以也請各位多多包涵。
所謂三體氣候模擬,就是將太陽出現的情況進行分類討論,然后將其呈現出來。比如說一顆太陽就是恒紀元,兩顆太陽可能是二日凌空或二日連珠,三顆太陽也可能是三日凌空或三日連珠。只要明白了這一點,這個三體氣候模擬的程序就很好寫了。
在寫程序前,得先導入一個庫。由于三體問題的復雜性,我們姑且將三顆太陽出現的概率定位三分之一,也就是說要用到隨機的方法。所以我們這里需要導入random庫中的randint——隨機數函數。
from random import randint
在插入完random庫后,要先確定幾個變量。由于三體世界有三顆太陽,且可能出現在不同的位置,所以姑且定義變量:
#其中0代表該太陽為飛星,1代表該太陽出現
sun1 = randint(0, 1)
sun2= randint(0, 1)
sun3= randint(0, 1)#1~3分別代表不同的位置,如果位置相同就是連珠
sun1_pos = randint(1, 3)
sun2_pos= randint(1, 3)
sun3_pos= randint(1, 3)
除了這幾個基礎變量,還需要兩個用來輸出氣候類型和紀元類型的變量:weather和era_mode
weather = ""era_mode= ""
因為后面將這兩個變量以文字形式輸出,所以是String的形式
完成變量設定后,就該考慮三體世界的氣候情況了。
依照書中的描述,我們可以分為(恒紀元就不討論了):三顆飛星(即沒有太陽)、二日凌空、二日連珠、三日凌空和三日連珠
這么一來,就可以開始寫程序了。
首先是沒有太陽,即三顆飛星情況:
if sun1 == sun2 == sun3 == 0: #三顆太陽都沒有出現
weather = "三顆飛星"era_mode= "亂紀元"
print(era_mode, weather) #輸出氣候情況
然后檢測是否為恒紀元,即一顆太陽:
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)
接著是三顆太陽的情況:
if sun1 == sun2 == sun3 == 1:if sun1_pos == sun2_pos ==sun3_pos:
weather= "三日連珠"era_mode= "亂紀元"
print(era_mode, weather)else:
weather= "三日凌空"era_mode= "亂紀元"
print(era_mode, weather)
最后是兩顆太陽的情況,就相對比較麻煩了:
if sun1 == sun2 == 1:if sun1_pos ==sun2_pos:
weather= "二日連珠"era_mode= "亂紀元"
print(era_mode, weather)else:
weather= "二日凌空"era_mode= "亂紀元"
print(era_mode, weather)elif sun1 == sun3 == 1:if sun1_pos ==sun2_pos:
weather= "二日連珠"era_mode= "亂紀元"
print(era_mode, weather)else:
weather= "二日凌空"era_mode= "亂紀元"
print(era_mode, weather)elif sun2 == sun3 == 1:if sun2_pos ==sun3_pos:
weather= "二日連珠"era_mode= "亂紀元"
print(era_mode, weather)else:
weather= "二日凌空"era_mode= "亂紀元"
print(era_mode, weather)
注意,這個從三顆飛星、一顆太陽、三顆太陽、兩顆太陽的順序是不能打亂的,否則就會出現氣候判斷不準的情況,因為這個程序的運行是從上往下走的,是線性的。舉個例子,如果現在有三顆太陽,而兩顆太陽的判定在三顆太陽的判定之前,程序運行時就會出現只輸出二日連珠或二日凌空的情況,而不會輸出三日凌空或三日連珠。
當然,你也可以用其他的方法讓氣候判斷的排序改變,比如可以全部把這些判斷啥的寫道不同的函數里在進行判斷,但在這里就不加以贅述。
最后把全部代碼放上來:
1 from random importrandint2
3 #其中0代表該太陽為飛星,1代表該太陽出現
4 sun1 = randint(0, 1)5 sun2 = randint(0, 1)6 sun3 = randint(0, 1)7 #1~3分別代表不同的位置,如果位置相同就是連珠
8 sun1_pos = randint(1, 3)9 sun2_pos = randint(1, 3)10 sun3_pos = randint(1, 3)11
12 weather = ""
13 era_mode = ""
14
15 if sun1 == sun2 == sun3 == 0: #三顆太陽都沒有出現
16 weather = "三顆飛星"
17 era_mode = "亂紀元"
18 print(era_mode, weather) #輸出氣候情況
19 elif sun1 == 1 and sun2 == sun3 ==0:20 era_mode = "恒紀元"
21 print(era_mode)22 elif sun2 == 1 and sun1 == sun3 ==0:23 era_mode = "恒紀元"
24 print(era_mode)25 elif sun3 == 1 and sun1 == sun2 ==0:26 era_mode = "恒紀元"
27 print(era_mode)28 elif sun1 == sun2 == sun3 == 1:29 if sun1_pos == sun2_pos ==sun3_pos:30 weather = "三日連珠"
31 era_mode = "亂紀元"
32 print(era_mode, weather)33 else:34 weather = "三日凌空"
35 era_mode = "亂紀元"
36 print(era_mode, weather)37 elif sun1 == sun2 == 1:38 if sun1_pos ==sun2_pos:39 weather = "二日連珠"
40 era_mode = "亂紀元"
41 print(era_mode, weather)42 else:43 weather = "二日凌空"
44 era_mode = "亂紀元"
45 print(era_mode, weather)46 elif sun1 == sun3 == 1:47 if sun1_pos ==sun2_pos:48 weather = "二日連珠"
49 era_mode = "亂紀元"
50 print(era_mode, weather)51 else:52 weather = "二日凌空"
53 era_mode = "亂紀元"
54 print(era_mode, weather)55 elif sun2 == sun3 == 1:56 if sun2_pos ==sun3_pos:57 weather = "二日連珠"
58 era_mode = "亂紀元"
59 print(era_mode, weather)60 else:61 weather = "二日凌空"
62 era_mode = "亂紀元"
63 print(era_mode, weather)
總行數不超過100行,還是挺精簡的
如果要查看太陽的生成,可以在添加完變量后(即上述代碼的11行處)添加:
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))
欸嘿?你問我print里輸入%是什么意思?python3 語法小記可以幫到你,畢竟這個語法還是蠻重要的,可以免去在print中加逗號所帶來的一格空格。
2020/3/3
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的用python模拟三体运动_怎么用Python写一个三体的气候模拟程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 多分类 recall_py
- 下一篇: qt 进度条_Qt开源作品12-硬盘容量