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