跳轉到內容

選擇你自己的Python冒險/舊目錄頁

來自華夏公益教科書,開放的書籍,開放的世界

本書是雙城ExCo(實驗學院)課程位元與位元組:程式設計入門的課程教材。

你認為程式設計師天生就手持鍵盤嗎?程式設計師是後天造就的,不是天生的——你也可以和最優秀的程式設計師一樣編碼。如果你有興趣打破程式設計周圍的障礙和神秘面紗,加入我們!在一個輕鬆、無壓力的環境中學習編碼。

你的輔導員,Gregg 和 Amanda,來自非傳統的程式設計背景,曾經也是新手。我們對精英技術宅、男性至上主義和宅男優越感毫無耐心。

我們的主要專案是一個網路應用程式,它允許你玩自己編寫的“選擇你自己的冒險”遊戲!(例如:http://cyoa.lind-beil.net/)。

所有教學都使用 Python 語言,這是一種免費的、開源的、跨平臺的、功能強大且易於學習的語言。我們將幫助你入門,每週介紹新的概念。會有動手練習、充足的提問時間和一個鬆散的結構。駭客精神關乎解放和民主化權力。

先決條件:能夠執行或安裝程式的計算機。僅線上學習也可以,但實際學習更好(你需要一臺筆記型電腦,或者非常強壯的胳膊才能搬動你的桌上型電腦)。

有經驗的程式設計師也歡迎作為學習者或導師。

請告訴我們關於移動性、神經多樣性或育兒需求方面的任何要求,我們將盡力滿足。

為什麼是另一本Python書?

[編輯 | 編輯原始碼]

我們受到以下內容的啟發

Kirrily Roberts 的 OSCON 演講Dreamwidth 的 Python vs. Ruby 死亡競賽.

關於作者

[編輯 | 編輯原始碼]

目標讀者

[編輯 | 編輯原始碼]

安裝Python

[編輯 | 編輯原始碼]

1a. Python,來自Python 網站

你需要最新的 2.x 系列(可能是 2.6.x)版本,而不是 3.x.x 版本。[1] Python 3 在某些方面有所不同(主要是在字串方面),本課程沒有涉及。

如果你更喜歡冒險,可以隨意嘗試頁面底部的其中一個安裝程式

  • ActiveState ActivePython(非開源)
  • Enthought Python Distribution(用於科學計算的商業發行版)
  • Portable Python(Python 和附加軟體包,配置為從行動式裝置執行)(如果你不能在系統範圍內安裝 Python,並且需要從 USB 驅動器、SD 卡或類似裝置執行,建議使用此選項)

這些發行版包含我們不會使用的其他模組(程式碼包)。如果你開始認真使用 Python,安裝其中一個包比逐個安裝要容易得多。使用通常的 Windows 方法安裝它。

1b. 測試你的 Python 安裝。

   start > run > cmd  [OK]
  

這將開啟一個 Windows cmd 視窗。在其中,輸入python

   C:\Documents and Settings\Gregg>python
   Python 2.4.3 - [some other info, perhaps]
   >>> 'hello'
   'hello'

如果你看到類似於以下內容,那麼你就可以了!

2a. 安裝文字編輯器。

文字處理器,包括 Microsoft Word 及其同類軟體,非常不適合編寫程式碼,因為它們混淆了佈局格式和文字。越簡單越好。也就是說,記事本也不適合,因為它會自動[2]在檔名後面新增“.txt”,以及其他一些功能。

一個好的程式設計編輯器的關鍵功能

  • 語法高亮顯示
  • 等寬字型
  • 多個選項卡式介面。

我們非常喜歡的一個免費(免費啤酒和開源)編輯器是 SciTE [1]。這個程式在SourceForge 上也有一個“無需安裝”版本。行動式版本沒有安裝版本的所有功能,但功能相當強大。一個好的跡象是程式可以以不需要安裝程式的形式存在。這意味著開發人員不想幹擾正在執行的系統,也不想損壞任何東西,並使你能夠輕鬆地刪除該程式,如果你不喜歡它的話。

2b. 測試你的安裝

雙擊 SciTE,或從程式選單中選擇它,或點選可執行檔案,以通常的 Windows 方式操作。你應該會得到一個類似記事本的工作區。

複製貼上以下內容:

 # here is some python code
 1 # an int
 b = 'some string'  # string
 if 2 > 1:  print "sure looks bigger"

然後,在選單中:語言 > Python。程式碼應該改變顏色,各個部分(變數、整數、字串、註釋)將變成漂亮的顏色。

Mac 使用者好訊息!Python 作為 Mac OS 的一部分預先安裝了。檢查一下

  1. 在 Finder 中,導航到應用程式 -> 實用工具 -> 終端並啟動終端
  2. 輸入 python 並按回車鍵
  3. 你將看到類似於以下內容
 Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) 
 GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
 Type "help", "copyright", "credits" or "license" for more information. (
 >>> print "hello"
 'hello'

你可以在 Python 命令提示符下完成很多操作,但使用文字編輯器編寫更復雜的問題會更容易。TextWrangler 是 Mac 使用者的一個不錯的文字編輯器。從Barebones Software 網站下載它

(可選)安裝ipython

Ipython 是一個 Python 包,它提供了一個更友好的命令列環境,包括語法高亮顯示、歷史記錄以及各種除錯工具和改進。從Ipython 網站下載它。

[編輯 | 編輯原始碼]

PyScripter 是一款免費的 IDE(適用於 Windows)。如果您有程式設計經驗,它與 Borland Delphi IDE 類似。您可以從 PyScipter 專案網站 下載 PyScripter。

print "Hello, world!"

執行後,您應該會看到

 "Hello, world!"

使用 Turtle 玩耍

Python 的 turtle 模組是 類似 LOGO 的語言 的一個簡單重新實現。

從您的 Python 提示符

# import everything from the turtle module
# import:  make them available for use
# everything:  (mostly) everything (there are some exceptions)
# the turtle module:  a collection of functions (actions), constants,
#    and other useful stuff that is grouped into one 'space' (a namespace)
#    named turtle, for easy of memory, and general good sense
>>> from turtle import *  
>>> circle(80)   # this will draw a circle with a diameter of 80
>>> reset()  # reset the screen
>>> forward(10)  # make the turtle go forward 10

所有命令都列在 Turtle 參考文件

沒有行號,但可以複製貼上

## anything from '#' is a comment, and gets ignored.  
## all my editorial comments will start with '##' -- GRL
 
## some text describng what this is
# a simple choose your own adventure
 
## 'print' prints to the screen.  
print "Welcome to MYSTERIOUS MANSION."
 
print "You are at a mysterious door.  The door is clearly marked -- 'Open Me And Die!'."  
 
## in python, strings can be single or double-quoted
print 'Do you want to open the door?'
 
## raw_input gets input from the user
## Here, we take the input, and *assign* it to a variable called 'ans'
ans = raw_input("please type 'yes' or 'no' ")
 
## conditionals
## see if the user's answer is interesting or not
if ans=="yes":
    print "That was foolish!  You are now dead."
## elif means "else-if"
elif ans == "no":
    print "That was wise!  You are alive, but thoroughly bored."
## else is a 'catch-all' for "any condition not all ready covered"
else:
    print "I don't know what to do, based on what you said, which was, |", ans, "|"
 
print "Thank you for playing!"
## anything from '#' is a comment, and gets ignored.  
## all my editorial comments will start with '##' -- GRL

## some text describng what this is
# a simple choose your own adventure

## 'print' prints to the screen.  
print "Welcome to MYSTERIOUS MANSION."

print "You are at a mysterious door.  The door is clearly marked -- 'Open Me And Die!'."

## in python, strings can be single or double-quoted
print 'Do you want to open the door?'

## raw_input gets input from the user
## Here, we take the input, and *assign* it to a variable called 'ans'
ans = raw_input("please type 'yes' or 'no' ")

## conditionals
## see if the user's answer is interesting or not
if ans=="yes":
    print "That was foolish!  You are now dead."
## elif means "else-if"
elif ans == "no":
    print "That was wise!  You are alive, but thoroughly bored."
## else is a 'catch-all' for "any condition not all ready covered"
else:
    print "I don't know what to do, based on what you said, which was, |", ans, "|"

print "Thank you for playing!"

其他頁面

[編輯 | 編輯原始碼]

參考資料

[編輯 | 編輯原始碼]
  1. 截至 2009 年 9 月 9 日:選擇 Python 2.6.2 Windows 安裝程式(Windows 二進位制檔案 - 不包括原始碼)
  2. 自動地駭客自動,彷彿是魔法。正如在幻想曲中,這可能是一件喜憂參半的事情。



請僅在書籍標題頁面新增 {{alphabetical}}

華夏公益教科書