6–1.
String. Is there a string method or function in the string module that can help me identify a string
Is it part of another large string?
Answer:in not in
6-2.
#! /usr/bin/env python# coding: utf-8 "6–2. String identifiers- idcheck.py of the modified example 6-1 Script so that it can detect an identifier of length one, and can identify Python keyword, for the latter requirement, you can use keyword module (especially keyword.kelist) To help you. " import stringimport keywordalphas = string.letters + ' _ ' nums = string.digitskey_list = keyword.kwlistprint ' welcome to the identifier checker v1.0 ' print ' Testees must be ai least 2 chars long. ' Myinput = raw_input (' Identifier to test? ') If len (myinput) >= 1: if myinput[0] not in alphas : print ' Invalid : first symbol must be alphabetic ' elif myInput in key_list: print ' invalid: the input&Nbsp;id is a python ' S keyword ' else: alphnums = alphas + nums for otherChar in myInput[1:]: if otherChar not in alphnums: print ' invalid: remaining symbols Must be alphanumeric ' break else: print "Okay as an identifier"
6-3.
#! /usr/bin/env python# coding: utf-8 ' 6–3. Sort (a) Enter a string of numbers, ranging from large to small. (b) is the same as a , but it is arranged in a dictionary order from large to small. " # (a) Def get_num (): global num_list num_list = [] num = ' while num != '! ': num = raw_input (' Enter some numbers to '! "End"). Strip () if num != '! ': try: num = float (num) except: print ' input is wrong, please re-enter ' get_num () else: num_list.append (num) else: break return num_listdef sort_descending (): get_num () print sorted (num_list, reverse = true) print '----------------(a)----------------' sort_descending () # (b) print '-----------------(b)--------- ------' Key_sort = []while true: k = raw_input (' Enter some numbers, Order size in dictionary order, end with "#" input: ') if k != ' # ': key_sort.append (k) else: Breakprint sorted (Key_sort,&nbsP;reverse = true)
Python core programming chapter sixth after-class exercises