Iron Music study python_day09_ Homework

Source: Internet
Author: User

Exercises

1, sorting function related knowledge points, writing blog2, write functions, check for the elements corresponding to all the odd-bit indexes of the incoming list or tuple object, and return them as new lists to the caller.defOdd_index (l): Lis=[] forIinch Range(Len(l)):# setting an index range by range is better than finding an index through an element because the index value returned by the same element is incorrect        ifI% 2 != 0: Lis.append (L[i])returnLis# Verify that the use of the Odd_index function is normal:Li=[I forIinch Range( -)]Print(LI)Print(Odd_index (LI)) tuple1=(' A ',' B ',' C ',' B ',' C ',' d ',' E ',' F ',' G ')Print(Odd_index (Tuple1)) [0,1,2,3,4,5,6,7,8,9,Ten, One, A, -, -, the, -, -, -, +][1,3,5,7,9, One, -, the, -, +][' B ',' B ',' d ',' F ']# It's easier to use slices# def Odd_index (argv):# return Argv[1::2]3and write functions to determine whether the length of the object passed in by the user (string, list, tuple) is greater than5。defLen_5 (L):return Len(l)> 54, write the function, check the length of the incoming list, if it is greater than2, only the first two-length content is retained and the new content is returned to the caller.defLen_2 (L):if Len(l)> 2:# satisfies the condition to return the content, does not satisfy the words to do nothing, namely none        returnl[:2]lis=[1,2]Print(Len_2 (LIS)) Lis2=[1,2,3,4]Print(Len_2 (LIS2))None[1,2]5, write the function, calculate the number of "number", "Letter", "Space", and "other" in the incoming string, and return the result.defCount_n (s): num= 0Alpha= 0Blank= 0Other= 0     forIinchSifI.isdigit (): num+= 1        elifI.isalpha (): Alpha+= 1        elifI== "': Blank+= 1        Else: Other+= 1Result= ' Total number {}, letter {}, Space {}, other {} '.format(Num,alpha,blank,other)return(Result)# ValidationPrint(Count_n (' uy143 oifg87 90/+ ')) Total number7A, letter6A, space6One, other2A6, write functions, check whether each element of a user's incoming object (string, list, tuple) contains empty content2, and returns the result.defBLANK_EX (l): Li=[] forIinchLif "' inch Str(i): Li.append (i)returnLi#验证S= ' Yiu dGH 'Li1=[' 34s ', $,' et ']tuple1=(' r t ', +, [ ])Print(BLANK_EX (s))Print(BLANK_EX (LI1))Print(BLANK_EX (Tuple1))7, write the function, check the length of each value passed in the dictionary, if it is greater than2, only the first two-length content is retained and the new content is returned to the caller. Dic={"K1":"V1v1","K2": [ One, A, -, -]} PS: value in the dictionary can only be a string or listdefDIC_EX (d): forIinchD:# by key to find the corresponding value, can be directly re-assignment        if Len(D[i])> 2: D[i]=d[i][:2]returnD# ValidationDic={"K1":"V1v1","K2": [ One, A, -, -],}Print(DIC_EX (DIC)) {' K1 ':' v1 ',' K2 ': [ One, A]}8, write a function, receive two numeric parameters, return the larger number.defMax_min (A, B):returnAifA>BElseB9, write the function, the user passes in the modified file name, with the content to modify, executes the function, completes the entire file the batch modification operation (advanced).Ten, write a function to complete three landing functions, and then write a function to complete the registration function (Advanced)# define Registration functionsdefRegister*Args**Kwargs): while True: username= input(' Please enter your username (q/q back to home page): '). Strip ()ifUsername.upper ()== ' Q ':# Exit Function            return         with Open(' Reg.txt ', encoding=' Utf-8 ') asF1: forIinchF1:user=I.strip (). Split ()ifUsername==user[0]orUsername== "':Print(' There is a user name, please re-register: ') Break            # An append-write operation is determined after the For loop has finished reading the entire file without a user of the same name            Else: Password= input(' Please enter your password: '). Strip ()# Extended a feature with a password that cannot be empty and less than 6 digits                ifPassword=="' or Len(password)< 6:Print(' The password cannot be empty or less than 6 bit, please re-register. ')Continue                 with Open(' Reg.txt ', encoding=' Utf-8 ', mode=' A ') asF2:f2.write ('\ n{}\ t{} '.format(username, password))Print(' registered success ')return# define a login functiondefLogin*Args**Kwargs): Count= 3     whileCount!= 0: username= input(' Please enter your user name: '). Strip () password= input(' Please enter your password: '). Strip () with Open(' Reg.txt ', encoding=' Utf-8 ') asF3: forIinchF3:user=I.strip (). Split ()ifUsername==user[0] andPassword==user[1]:Print(' Login successful ')# Login Successful sign out function                    return            Else: Count-= 1                Print(' your username or password entered incorrectly, please re-enter. You still have%dopportunity. ' %CountPrint(' Your three-time input is exhausted, please try again later. ') Tomorrow's dictation. The role of ①,return. ②, there are several ways to pass a parameter, each of which simply writes a code. For example, arguments are passed by location.defFunc (x, y):P assfunc (' A ', ' B ')

Iron Music study python_day09_ Homework

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.