2024年3月26日发(作者:)

def idcheck():

'''6-2 修改idcheck使之可以检测长度为一得标识符,并且可识别

Python 关键字

'''

import string

import keyword

keys =

alphas = s+'_'

nums =

alphanums = alphas+nums

print 'Welcome to the Identifier Check V1.1'

myInput = raw_input('identifier to test: ')

isOne = False # 是否是一个字符

if len(myInput) == 1:isOne = False

if myInput in keys:

print '''invalid:symbol has been defined'''

return False

elif myInput[0] not in alphas:

print '''invalid:first symbol must be alphabetic'''

return False

elif not isOne:

otherInput = myInput[1:]

for otherChar in otherInput:

if otherChar not in alphanums:

print '''invalid:remainn symbols must be alphanumeric'''

return False

print "okay as an Identifier"

return True

def order(nlist):

'''6-3(a) 输入一串数字,从大到小排列

注意:输入的是一个列表,其中的值为数字

'''

newlist = []

for x in nlist:

(int(x))

return sorted(newlist,reverse=True)

def order2(nlist):

'''6-3(b) 和a一样,不过要用字典序

注意:字典序就是按字典的排列方式,比如 21 就大于 111 ,因为 2大于1

方式就是把输入的数字变成字符串即可

'''

# 将里面的元素统统变成字符串先

newlist = []

for x in nlist:

(str(x))

newlist = sorted(newlist,reverse=True)

for i,x in enumerate(newlist):

newlist[i] = int(x)

return newlist

def avescore():

'''输入测试得分,算出平均值'''

scorelist = [] # 分数列表

while True:

myinput = raw_input('Input the score(if No number quite): ')

try:

(float(myinput))

except:

break

if not len(scorelist):return False

return sum(scorelist)/len(scorelist)

def showstr():

'''6-5(a) 更新2-7 使之可以每次向前向后都显示一个字符串的一个字符'''

istr = raw_input('Input string: ')

lens = len(istr)

if lens==0:return False

if lens==1:

print istr

return True

for i,j in enumerate(istr):

if i ==0 and len(istr)!=1:

print j,istr[i+1]