python核心程式設計學習(一)

2021-06-04 16:02:05 字數 946 閱讀 4150

以下是核心程式設計中乙個stack的例子:

'''

created on 2012-3-6

@author: administrator

'''#!/usr/bin/env python

stack=

def pu****():

def popit():

if len(stack)==0:

print 'has empty!'

else:

print 'remove [', stack.pop() ,']'

def viewstack():

print stack

cmds =

def showmenu():

pr="""

p(u)sh

p(o)p

(v)iew

(q)uit

enter choice : """

while true:

while true:

try:

choice = raw_input(pr).strip()[0].lower()

except (eoferror,keyboardinterrupt,indexerror):

choice = 'q'

print '\nyou picked:[%s]' % choice

if choice not in 'uovq':

print 'invalid option,try again'

else:

break

if choice =='q':

break

cmds[choice]()

if __name__=='__main__':

showmenu()

請切記python程式設計中空格的重要性哦!

python核心程式設計學習(三)

核心程式設計中的檔案操作的例子 created on 2012 3 7 author administrator usr bin env python import os for tmpdir in c r if os.path.isdir tmpdir print tmpdir break els...

python核心程式設計學習(四)

乙個計算簡單加減法的例子 created on 2012 3 7 author administrator usr bin env python from operator import add,sub from random import randint,choice ops maxtries 2...

python核心程式設計學習(六)

乙個函式傳引數的例子 created on 2012 3 8 author administrator usr bin env python def testit func,nkwargs,kwargs try retval func nkwargs,kwargs result true,retva...