Python core programming Exercises-sixth chapter

Source: Internet
Author: User

1. String, is there a string method or function in the string module that can help me identify if a string is part of another large string?

 1  str1 =  " abcdefghijklmnopqrstuv    2   '    ' #若count数量大于0 means that the substring is part of the str1.  4   '  

2. String identifier, modify the 6-1 idcheck.py script, so that it can detect the length of one identifier, and can identify the Python keyword, for the latter, you can use the keyword module (especially keyword.kwlist) to assist

1 Importstring2 Importkeyword3 4Alphas = String.letters +'_'5Nums =string.digits6KW =keyword.kwlist7 8 Print 'Welcome to the Identifier Checker v1.0'9 Print "Testees must is at least 2 chars long."TenMyinput = Raw_input ('indetifier to test -') One  A ifLen (myinput) = = 1: -     ifMYINPUT[0] not inchAlphas: -         Print "Invalid:first symbol must be alphabetic" the     Else: -         Print "Ok as an identifier length 1"         -  - elifLen (myinput) > 1:     +     ifMYINPUT[0] not inchAlphas: -         Print "Invalid:first symbol must be alphabetic" +     elifMyinputinchKW: A         Print "invalid:it a python keyword" at     Else: -          forOthercharinchMyinput[1:]: -             ifOtherchar not inchAlphas +Nums: -                 Print "invalid:remaining symbols must be alphanumeric" -                  Break -         Else: in             Print "Okay as an identifier"

3. Sorting

(a) Enter a series of numbers and arrange them from large to small.

(b) Same as a, but in dictionary order from large to small

4. Arithmetic. Update the previous chapter with your scoring test practice plan, put the test scores in a list, and your code should be able to calculate an average score. See Contacts 2-9 and 5-3

1 defScorecalculate (score):2     ifScore < 60:3Rank ='F'4     elifScore < 70:5Rank ='D'6     elifScore < 80:7Rank ='C'8     elifScore < 90:9Rank ='B'Ten     Else: OneRank ='A' A     returnRank -  -Yourscore = [] the  whileTrue: -Score = Raw_input ("Enter your score (Q for Quit)--") -     ifScore = ='Q': -          Break +     Else: - yourscore.append (int (score)) +Average = float (sum (yourscore))/Len (yourscore) A PrintAverage

5. String

(a) Update your program in exercise 2-7 so that it can display one character of a string before and after each.

  

Python core Programming Exercises-sixth chapter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.