Codecademy这个在线学习平台上提供的Python教程非常友好,让初学者非常容易上手,而且每练习一些题目,网站还给你发个奖章鼓励鼓励你继续进行下去,我觉得这个网站做得挺用心的。
这个网站的练习做完了,开始学习Coursera上的Python课程了,这个课程的最终目标是让你可以编一个飞船射击的游戏,更加侧重网页版的GUI编程。虽然我对这个GUI并不太感兴趣,不过也注册了这个课程,就当做继续练习Python语法了吧。
下面的笔记内容来自coursera上的Python公开课。
Event-driven Programming model
keep waiting forever until an event occur.
event1….event999
Start — initiate — wait
handler1…handler999
Event:
1 keyboard
2 input
3 mouse
4 timer
Example of a simple event-driven program
import simplegui # Event handler def tick(): print "tick!" # Register handler timer = simplegui.create_timer(1000, tick) # Start timer timer.start()
1 尽可能地使用local variables
2 global variables的好处在于,global variables are an easy way for event handlers to communicate game information.但是更好的办法是使用 OOP
# global vs local examples # num1 is a global variable num1 = 1 print num1 # num2 is a local variable def fun(): num1 = 2 #这里的num1虽然和line 5的全局变量num1是同名的,但是其实本质是local variable。在function()中的变量都是local variable num2 = num1 + 1 print num2 fun() # example num = 4 def fun1(): global num num = 5 def fun2(): global num num = 6 # note that num changes after each call with no obvious explanation print num fun1() print num fun2() print num
# SimpleGUI program template # Import the module import simplegui # Define global variables (program state) counter=0 # Define "helper" functions def increment(): global counter counter=counter+1 # Define event handler functions def tick(): increment() print counter def buttionpress(): global counter counter=0 # Create a frame frame=simplegui.create_frame("SimpleGui Test", 100, 100) # Register event handlers timer=simplegui.create_timer(1000,tick) frame.add_button("click me",buttionpress) # Start frame and timers frame.start() timer.start()
# calculator with all buttons import simplegui # intialize globals store = 0 operand = 0 # event handlers for calculator with a store and operand def output(): """prints contents of store and operand""" print "Store = ", store print "Operand = ", operand print "" def swap(): """ swap contents of store and operand""" global store, operand store, operand = operand, store output() def add(): """ add operand to store""" global store store = store + operand output() def sub(): """ subtract operand from store""" global store store = store - operand output() def mult(): """ multiply store by operand""" global store store = store * operand output() def div(): """ divide store by operand""" global store store = store / operand output() def enter(t): """ enter a new operand""" global operand #错误1 没有将输入字符串转换为数字型 #operand = t #错误2 如果将字符串转为int型,将不能接受float型输入 #operand = int(t) #正确写法 operand = float(t) output() # create frame f = simplegui.create_frame("Calculator",300,300) # register event handlers and create control elements f.add_button("Print", output, 100) f.add_button("Swap", swap, 100) f.add_button("Add", add, 100) f.add_button("Sub", sub, 100) f.add_button("Mult", mult, 100) f.add_button("Div", div, 100) f.add_input("Enter", enter, 100) # get frame rolling f.start()For, debugging, you should remember that building an initial version of your project that prints information in the console is a development strategy that you should use in later projects as well.