or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
Python實作課程
第一堂課 : 猜數字遊戲
2022 / 3 / 24
Tony
硬體設備
學校可以寫python的編輯器是Spyder

左方為撰寫程式的地方,右下方為終端機
猜數字遊戲
猜數字遊戲(Guess the number) a.k.a 終極密碼
是一個在特定數字範圍內
猜到出題者所指定數字的遊戲
每次的猜測都會讓範圍縮小,直到答案被猜到
遊戲規則
(即程式要實現的東西)
開始寫程式
你可以下載模板:點我
主要要寫的三個函式:
課堂上會寫猜數字函式I,猜數字函式II的部份各位有興趣可以自己寫寫看
參考答案下禮拜會公布
主函式
Python沒有強迫要有像C++
int main()
的函式,通常就是直接運行你打出的程式碼(函式除外)但也可以自己創造出主函式,利用
if __name__ == '__main__':
可以幫助整理程式碼,看起來比較整齊,也會知道哪些程式會跑哪些不會
猜數字函式 I
(電腦出題使用者猜)
程式執行程序:
使用工具:random
我們模擬電腦在選答案,或是在猜測答案的時候,都是讓它在一個範圍內隨機選一個數字
故我們要利用random資料包裡面的
random.randint()
函式,隨機挑出一個數字f string
我們需要在輸出的字串中放入程式中某些變數的內容,
此時就可以使用f string,把變數內容用大括號包起來
下一頁是完整的code內容
想挑戰自己寫的先不要往下看