In addition to 30 keywords or so, Python-3 allows you to use non-English characters as identifiers. That means you can write a program almost in your own native language, which is usually the one you feel the most fluent to speak and to THINK.
It is an interesting option for me, I enjoy it, and love it. I suggest you to give it a try.
There are several discussion about this idea as follows:
- http://www.reddit.com/r/Python/comments/268fts/a_python_program_written_with_90_traditional/
- https://plus.google.com/+SebastianRaschka/posts/2ThNvMEeyWD
A bunch of examples are here.
- https://dl.dropboxusercontent.com/u/33089565/ry2014_thinkcspy/html/_ryTurtle04.html
- https://github.com/renyuanL/pythonTurtleInChinese
有沒有試過用 中文 寫程式?
Python-3 除了 約 30個 關鍵字(keywords) (列在文後當參考)不可用中文之外,
其他部分允許程式員使用中文。
(其實任何使用 Unicode 編碼的語言皆可,
我偶爾也會用一下希臘字母,或日文假名。)
中文 是我最流利(fluent)的語言,
也是我的華人朋友們(包含大朋友、小朋友以及老朋友)最易懂的語言,
用中文來與這些朋友們溝通(包括我自己的自言自語),
是最有效率、最不易誤解的語言。
誠意推薦你也來試試看。
會有意想不到的收穫喔!
例子如下:
# myNumber.py, 我的數字遊戲.py, # by Chris Roffey, 翻譯:呂仁園。 # 本遊戲使用自製的函數 import random # 想一個數字 電腦數字= random.randint(1, 100) # 創造一個函數,相同嗎() def 相同嗎(目標, 數字): if 目標 == 數字: 結果= "贏" elif 目標 > 數字: 結果= "低" else: 結果= "高" return 結果 # 開始遊戲 print("你好。\n我已經想好一個數字,介於1和100之間。") # 取得使用者的猜測,並轉成整數。 猜測= int(input("你能猜到它嗎?")) # 運用我們的函數 高或低= 相同嗎(電腦數字, 猜測) # 跑遊戲,直到使用者猜對。 while 高或低 != "贏": if 高或低 == "低": 猜測= int(input("抱歉,你猜得太低了,再一次吧!")) else: 猜測= int(input("抱歉,你猜得太高了,再一次吧! ")) 高或低 = 相同嗎(電腦數字, 猜測) # 結束遊戲 print("正確!\n很棒!\n\n\n按 Enter 來離開。") input() |
# myNumber.py, # by Chris Roffey # This game uses a home made function import random # Think of a number computer_number = random.randint(1, 100) # Create the function is_same() def is_same(target, number): if target == number: result="Win" elif target > number: result="Low" else: result="High" return result # Start the game print("Hello.\nI have thought of a number between 1 and 100.") # Collect the user’s guess as an integer guess = int(input("Can you guess it? ")) # Use our function higher_or_lower = is_same(computer_number, guess) # Run the game until the user is correct while higher_or_lower != "Win": if higher_or_lower == "Low": guess = int(input("Sorry, you are too low. Try again. ")) else: guess = int(input("Sorry, you are too high. Try again. ")) higher_or_lower = is_same(computer_number, guess) # End the game print("Correct!\nWell Done\n\n\nPress Enter to exit.") input() |
# # myEtchASketch.py, 我的蝕刻繪圖板.py # a application from Coding Club: Python Basics # by Chris Roffey, 翻譯:呂仁園。 # from tkinter import * ##### 設定變數: 布高= 400 布寬= 600 布色= "black" 點x= 布寬/2 點y= 布高 點色= "green" 點寬= 5 點長= 5 ##### 函數: # # 玩家控制 # def 點移上(self): global 點y 布.create_line(點x, 點y, 點x, (點y-點長), width= 點寬, fill= 點色) 點y= 點y - 點長 def 點移下(self): global 點y 布.create_line(點x, 點y, 點x, 點y+點長, width= 點寬, fill= 點色) 點y= 點y + 點長 def 點移右(self): global 點x 布.create_line(點x, 點y, 點x + 點長, 點y, width= 點寬, fill= 點色) 點x= 點x + 點長 def 點移左(self): global 點x 布.create_line(點x, 點y, 點x - 點長, 點y, width= 點寬, fill= 點色) 點x= 點x - 點長 def 清除全部(self): 布.delete(ALL) ##### 主程式: 窗= Tk() 窗.title("我的蝕刻繪圖板") 布= Canvas( bg= 布色, height= 布高, width= 布寬, highlightthickness= 0) 布.pack() # # 綁定(連結) 按鍵 與 玩家控制函數 # 窗.bind("<Up>", 點移上) 窗.bind("<Down>", 點移下) 窗.bind("<Left>", 點移左) 窗.bind("<Right>", 點移右) 窗.bind("u", 清除全部) # # 進入視窗應用程式的主迴圈 # 窗.mainloop() |
# # myEtchASketch.py # a application from Coding Club: Python Basics # by Chris Roffey # from tkinter import * ##### Set variables: canvas_height = 400 canvas_width = 600 canvas_colour = "black" p1_x = canvas_width/2 p1_y = canvas_height p1_colour = "green" line_width = 5 line_length = 5 ##### Functions: # # player controls # def p1_move_N(self): global p1_y canvas.create_line(p1_x, p1_y, p1_x, (p1_y-line_length), width=line_width, fill=p1_colour) p1_y = p1_y - line_length def p1_move_S(self): global p1_y canvas.create_line(p1_x, p1_y, p1_x, p1_y+line_length, width=line_width, fill=p1_colour) p1_y = p1_y + line_length def p1_move_E(self): global p1_x canvas.create_line(p1_x, p1_y, p1_x + line_length, p1_y, width=line_width, fill=p1_colour) p1_x = p1_x + line_length def p1_move_W(self): global p1_x canvas.create_line(p1_x, p1_y, p1_x - line_length, p1_y, width=line_width, fill=p1_colour) p1_x = p1_x - line_length def erase_all(self): canvas.delete(ALL) ##### main: window = Tk() window.title("MyEtchASketch") canvas = Canvas( bg=canvas_colour, height=canvas_height, width=canvas_width, highlightthickness=0) canvas.pack() # # bind movement to key presses # window.bind("<Up>", p1_move_N) window.bind("<Down>", p1_move_S) window.bind("<Left>", p1_move_W) window.bind("<Right>", p1_move_E) window.bind("u", erase_all) # # entering mainloop of the window application # window.mainloop() |
# # rySketch.py, 繪圖板.py # 呂仁園。 # import tkinter def 在滑鼠座標處畫圓(e): x, y= e.x, e.y 布.create_oval( x-5, y-5, x+5, y+5, fill= 'red') def 清除畫布(e): 布.delete('all') 布= tkinter.Canvas() 布.pack() 布.bind('<1>', 在滑鼠座標處畫圓) 布.bind('<3>', 清除畫布) 布.bind('<B1-Motion>', 在滑鼠座標處畫圓) 布.mainloop() |
沒有留言:
張貼留言