class1.py
main.pyfrom random import choice
class
randomwalk
:def
__init__
(self, num_points=
5000):
self.num_points = num_points
self.x_value =[0
] self.y_value =[0
]def
fill_walk
(self)
:while
len(self.x_value)
< self.num_points:
x_step = self.get_step(
) y_step = self.get_step(
)if x_step ==
0and y_step ==0:
continue
next_x = self.x_value[-1
]+ x_step
next_y = self.y_value[-1
]+ y_step
defget_step
(self)
: direction = choice([1
,-1]
) distance = choice([0
,1,2
,3,4
])step = direction * distance
return step
'''主程式'''
說明:根據點先後出現的順序,顏色由淺入深,先出現的點顏色較淺,後出現的點顏色加深
儲存的python語句要在plt.show()之前,否則會儲存一片空白(這個坑我先踩為敬)
class2.py
from random import randint
class
die:
'''這是乙個骰子?'''
def__init__
(self, num_sides=6)
: self.num_sides =
int(num_sides)
defroll
(self)
:return randint(
1, self.num_sides)
main2.py
import pygal
from part15.die_ import die
die_1 = die(6)
die_2 = die(6)
die_3 = die(6)
results =
for roll_time in
range
(100):
result = die_1.roll(
)+ die_2.roll(
)+ die_3.roll(
)frequencies =
max_num = die_1.num_sides + die_2.num_sides + die_3.num_sides
for value in
range(3
, max_num)
: frequency = results.count(value)
hist = pygal.bar(
)hist.title =
'result of rolling one d6 100 times'
hist.x_labels =
[x for x in
range(3
,18)]
hist._x_title =
'result'
hist._y_title =
'frequency of result'
hist.add(
'd6'
, frequencies)
hist.render_to_file(
'three_dices.svg'
)
最終結果:
說明:
Python程式設計 從入門到實踐 1
內容總結自 python程式設計 從入門到實踐 安裝python3 安裝文字編輯器sublime text並配置python3環境 安裝sublime text tools new build system 將 untitled.sublime build 文件中的所有內容刪除,輸入以下內容 注意,...
《Python程式設計 從入門到實踐》 1
2.變數和簡單資料型別 mystr this is a string print mystr 引號括起的都是字串,可以單引號,也可以雙引號。單引號內能帶雙引號,不能帶單引號,反之亦然。mystr.title mystr字串的每個單詞的首字母都大寫,其他字母都小寫 mystr.upper mystr字...
python從入門到 (1)
編譯型語言代表 c語言 原始碼 編譯 編譯後的機器碼 特點 執 速度特別快,但跨平台性 較差 解釋型語言代表 python 原始碼 直譯器 解釋執 特點 執 速度 較慢,但跨平台性 較好 命令 的互動 式 text based user inte ce tui 圖形界 化的互動 式 graphica...