第六章

    xiaoxiao2021-03-25  102

    第2题 #!/usr/bin/env pythonimport stringimport keywordfirstLetter = string.letters + '_'letterOnLevel = firstLetter + string.digitsprint "Please Input Words To Test:",testWord = raw_input()if len(testWord) == 1: if testWord in firstLetter: print "%s Is On Level."%testWord else: print "%s Isn't On Level."%testWord exit()if testWord[0] not in firstLetter: print "%s Isn't On Level."%testWord exit()for ch in testWord[1:]: if ch not in letterOnLevel: print "%s Isn't On Level."%testWord exit()if testWord in keyword.kwlist: print "%s Is An Keyword,And It's Not On Level."%testWordelse: print "%s Is On Level."%testWord

    第3题

    #!/usr/bin/env pythonimport stringar = []Count = 0switches = Truewhile switches: print "Please Input %dth Digit:"%(Count+1), chs = raw_input() for ch in chs: #test the inputs is all digit or not if ch not in string.digits: switches = False break if switches == False: continue ar.append(int(chs)) Count += 1print arar.sort()print arar[-1::-1] = arprint ar

    第4题

    #!/usr/bin/env pythonexamList = []Count = 0while Count != 5: print "Please Input %dth Result Of Exam:"%(Count+1), chs = raw_input() examList.append(float(chs)) Count += 1avgExam = float(sum(examList)) / 5print"Result Is %.2f "%avgExamif avgExam <= 100 and avgExam >= 90: print "A"elif avgExam < 90 and avgExam >= 80: print "B"elif avgExam < 80 and avgExam >= 70: print "C"elif avgExam < 70 and avgExam >= 60: print "D"elif avgExam < 60: print "E"

    第5题

    #!/usr/bin/env pythonprint"Please Input Any Word To Test:",testWord = raw_input()for ch in testWord: print"%s"%ch,printfor ch in testWord[-1::-1]: print"%s"%ch,

    #!/usr/bin/env pythonprint "Please Input First Word:",word_1 = raw_input()print "Please Input Second Word:",word_2 = raw_input()if len(word_1) != len(word_2): print"!=" exit()Count = 0for ch in word_1: if ch != word_2[Count]: print"!=" exit() Count += 1print "=="

    #!/usr/bin/env pythonprint "Please Input Any Word To Test:"testWord = raw_input()Count = len(testWord) - 1for ch in testWord: if ch == testWord[Count]: Count -= 1 else: breakif Count == -1: print "=="else: print "!="

    #!/usr/bin/env pythonprint "PLease Input Any Word To Test:",testWord = raw_input()rTestWord = ""for ch in testWord[-1::-1]: rTestWord += chtestWord += rTestWordprint testWord

    第6题 #!/usr/bin/env pythondef myStrip(strs): newString = "" Lens = len(strs) if strs[0] == " " and strs[-1] == " ": for ch in strs[1:Lens-1]: newString += ch return newString if strs[0] == " ": for ch in strs[1::]: newString += ch return newString if strs[-1] == " ": for ch in strs[:Lens-1:]: newString += ch return newString return strsprint "PLease Input Any Word To Test:",testWord = raw_input()print testWordprint "~~~~~~%s~~~~~"%(myStrip(testWord))

    第7题

    #!/usr/bin/env pythonnum_str = raw_input("Enter a number:")num_num = int(num_str)fac_list = range(1,num_num+1)print fac_listi = 0Count = 0allNu = len(fac_list)while i < allNu: if num_num

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

    最新回复(0)