Python Learning (6)

Source: Internet
Author: User

Exercise 1:
Set a user name and password, the user entered the correct user name and password, the display login is successful, otherwise prompt logon failure, the user failed the most 3 times, or quit the program.
Tip: Use while or for to limit the number of retries, use input to get user input using? = To determine the user's user name and password.
#encoding =utf-8
Mode 1:

username = "hhq"password ="123456"for i in range(3):    user = input("please input the username: ")    passwd = input("please input the passwd: ")    if user == username and passwd == password:        print("guess ok")        break    else:        print("guess error")    if i ==2:#输错3次退出        print("input times is used out! bye!!")

Mode 2:

username = "hhq"password ="123456"for i in range(3):    user = input("please input the username: ")    passwd = input("please input the passwd: ")    if user == username and passwd == password:        print("guess ok")        break    else:        print("guess error")else:print("input times is used out! bye!!")

Exercise 2:

Implement a function yourself, find the algorithm of a word in a sentence, there is a return index number, otherwise return false
Tip: Use the coordinates in the sentence to traverse each position of the sentence, using the length of the find word to find the word in combination with the slice. Example: S[i:i+len (word)]

def findWord(s,word):    length = len(word)    for i in range(len(s)-length+1):        if s[i:i+length] == word:            return i    return -1print(findWord("I am a good boy","good"))print(findWord("I am good","good"))

Exercise 3:

Randomly generated an integer, 1-100 you guess the most 5 times, if the big guess, the hint is small, the hint is small, guessed right, prompt guess. 5 times not guessed, guess not guessed.

import randomtarget_num = random.randint(1,100)for i in range(5):    user_input_num = int(input("请输入你猜的数字: "))    if target_num == user_input_num:        print("你猜中了,数字是:",user_input_num)        print("你猜了%d 次" %i+1)        break    elif target_num > user_input_num:        print("你猜小了")    else:        print("你猜大了")    if i==4:        print("5次机会用光了 Bye")

Exercise 4:
Use while to calculate the sum of random numbers, over 100 of the time, to stop the program. The range of the random number 1-20 is generated, requiring the recording of the resulting random number, as well as the last and the number of random numbers.
Mode 1:

import randomresult =0random_num_list =[]while 1:    random_num = random.randint(1,20)    random_num_list.append(random_num )    result+=random_num    if result >100:        breakprint("一共产生了 %s 个随机数:" %len(random_num_list))print("产生随机数如下:",random_num_list)print("最后的随机数之和:",result)

Mode 2:

#encoding=utf-8import randomresult = 0number_list = []while result <= 100:    num = random.randint(1,20)    number_list.append(num)    result += numprint("产生的随机数:",number_list)print("随机数的和:",result)print("随机数的个数:",len(number_list))

Exercise 5:
Traverse strings, lists, respectively, based on location and character-based traversal

#encoding=utf-8import randoms = "abc"for i in s:    print(i)for i in range(len(s)):print(s[i])l = [1,2,3]for i in l:    print(i)for i in range(len(l)):print(l[i])

Exercise 6:
Traverse a list of nested lists and all elements of a tuple, outputting 1-12 of numbers! [[[1,2,3],4,5],7,8, (9,10, (11,12))]

import randoml = [[[1,2,3],4,5],7,8,(9,10,(11,12))]for value in l:    if isinstance(value,(list,tuple)):        for v in value:            if isinstance(v,(list,tuple)):                for j in v:                    print(j)            else:                   print(v)    else:        print(value)

Python Learning (6)

Related Article

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.