怎麼用Python寫乙個三體的氣候模擬程式

2021-10-07 21:45:03 字數 4604 閱讀 1992

所謂三體氣候模擬,就是將太陽出現的情況進行分類討論,然後將其呈現出來。比如說一顆太陽就是恆紀元,兩顆太陽可能是二日凌空或二日連珠,三顆太陽也可能是三日凌空或三日連珠。只要明白了這一點,這個三體氣候模擬的程式就很好寫了。

在寫程式前,得先導入乙個庫。由於三體問題的複雜性,我們姑且將三顆太陽出現的概率定位三分之一,也就是說要用到隨機的方法。所以我們這裡需要匯入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)

注意,這個從三顆飛星、一顆太陽、三顆太陽、兩顆太陽的順序是不能打亂的,否則就會出現氣候判斷不准的情況,因為這個程式的執行是從上往下走的,是線性的。舉個例子,如果現在有三顆太陽,而兩顆太陽的判定在三顆太陽的判定之前,程式執行時就會出現只輸出二日連珠或二日凌空的情況,而不會輸出三日凌空或三日連珠。

當然,你也可以用其他的方法讓氣候判斷的排序改變,比如可以全部把這些判斷啥的寫道不同的函式裡在進行判斷,但在這裡就不加以贅述。

最後把全部**放上來:

from random import randint

# 其中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 = ""

if sun1 == sun2 == sun3 == 0: # 三顆太陽都沒有出現

weather = "三顆飛星"

era_mode = "亂紀元"

print(era_mode, weather) # 輸出氣候情況

elif 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)

elif 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)

elif 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)

總行數不超過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))

用python寫乙個restful API

coding utf 8 package.module python實現的圖書的乙個restful api.restful api 一般模式 get select 從伺服器取出資源 一項或多項 post create 在伺服器新建乙個資源。put update 在伺服器更新資源 客戶端提供改變後的完...

python寫乙個服務 Python寫乙個服務

coding utf 8 import json from urllib.parse import parse qs from wsgiref.server import make server 定義函式,引數是函式的兩個引數,都是python本身定義的,預設就行了。定義檔案請求的型別和當前請求成功...

用python寫乙個蛇形矩陣

蛇形矩陣,如 10 11 12 1 9 16 13 2 8 15 14 3 7 6 5 4從右上角大回環,其實挺簡單,思路想明白了就順了。這樣的矩陣可以看做二維陣列,python對陣列的寫法很麻煩,用numpy生成就簡單多了 myarray np.zeros n,n dtype np.int16 有...