python 核心编程 第十一章

    xiaoxiao2021-03-25  125

    第3题 #!/usr/bin/env pythondef max2(x,y): return x if x>y else ydef min2(x,y): return x if x<y else yprint max2(8,4)print min2(8,4)def my_max(anyList): try: #Be sure the list isn't empty a = anyList[0] except Exception , docs: print str(docs) else: for L in anyList: a = a if a>L else L return adef my_min(anyList): try: a = anyList[0] except Exception , docs: print str(docs) else: for L in anyList: a = a if a<L else L return aprint my_max("123456765")print my_min("bcdaoe")

    第4题

    #!/usr/bin/env pythondef changeTime(H,M): timeH = H + float(M)/float(60) timeM = H*60 + M return [timeH,timeM]print changeTime(3,10)

    第5题

    #!/usr/bin/env pythondef GD(G,pt = 0.1): return G*ptprint GD(100)print GD(100,0.3)

    第6题

    #!/usr/bin/env pythondef printf(strings,*lists): LEN = len(strings) listKey = ['d','f','s'] nuEle = 0 nuEle = strings.count("%f") + strings.count("%d") + strings.count("%s")# for ch in strings:# if ch == "%" and strings.index(ch) != LEN-1: #The Code print strings.index(ch) aways show the index of firs"%"# nextCh = strings[strings.index(ch)+1] #I don,t know what happen...# if nextCh in listKey:# nuEle += 1 if nuEle != len(lists[0]): print "Wrong Element." return resultString = "" i = 0 iterStr = 0 while True: if strings[iterStr] == "%": try: nextChs = strings[iterStr+1] #Be sure not out of range except Exception: resultString += strings[iterStr] break nextChs = strings[iterStr+1] resultString += str(lists[0][i]) iterStr += 2 i += 1 else: resultString += strings[iterStr] iterStr += 1 if iterStr == LEN: break print resultStringprintf("abc

    转载请注明原文地址: https://ju.6miu.com/read-15342.html

    最新回复(0)