0016 _ exercise question d2, 0016 exercise question d2
1 _ author _ = 'prop 93 '2 #! /Usr/bin/env python 3 #-*-coding: UTF-8-*-4 # element classification, which has the following values: [, 44, 99, 90...], 5 # Save all values greater than 66 to the first key of the dictionary. 6 # Save values smaller than 66 to the second kye. 7 #{'k1 ': all values greater than 66, 'k2': All values smaller than 66} 8 9 l = [, 44, 99] 10 t1 = [] 11 t2 = [] 12 d = {} 13 for I in l: 14 if I> 66: 15 t1.insert (len (t1), I) # Use append to add 16 elif I <66: 17 t2.insert (len (t2), I) # generally use append to add 18 else: 19 pass20 d ['k1 '] = t121 d ['k2'] = t222 print (d)Method 1
1 _ author _ = 'prop 93 '2 #! /Usr/bin/env python 3 #-*-coding: UTF-8-*-4 # element classification, which has the following values: [, 44, 99, 90...], 5 # Save all values greater than 66 to the first key of the dictionary. 6 # Save values smaller than 66 to the second kye. 7 #{'k1 ': all values greater than 66, 'k2': All values smaller than 66} 8 9 l = [, 44, 99] 10 # t1 = [] 11 # t2 = [] 12 # d = {} 13 # for I in l: 14 # if I> 66: 15 # t1.insert (len (t1), I) # Use append to add 16 # elif I <66: 17 # t2.insert (len (t2), I) # generally, append is used to add 18 # else: 19 # pass20 # d ['k1 '] = t121 # d ['k2'] = t222 # print (d) 23 24 d = {'k1 ': [], 'k2': []} 25 for I in l: 26 if I> 66: 27 d ['k1']. append (I) 28 else: 29 d ['k2']. append (I) 30 31 print (d)Method 2
1 _ author _ = 'prop 93 '2 #! /Usr/bin/env python 3 #-*-coding: UTF-8-*-4 5 # Find the elements in the list and remove spaces, search for all elements starting with a or A and ending with c 6 7 li = ['alec ', 'aric', 'Alex ', 'Tony ', 'rain '] 8 tu = ('alec', 'aric ', 'Alex', 'Tony ', 'rain') 9 dic = {'k1 ': 'Alex ', 'k2': 'aric ', 'k3': 'Alex ', 'k4': 'Tony '} 10 11 for I in li: 12 x = I. strip () 13 if x. endswith ('C'): 14 x = x. upper () 15 if x. startswith ('A'): 16 print (I. strip ())Code Implementation
1 _ author _ = 'prop 93 '2 #! /Usr/bin/env python 3 #-*-coding: UTF-8-*-4 5 # output product list, user input serial number, show the items selected by the user 6 7 li = ['cell phone ', 'computer', 'mouse pad ', 'yacht'] 8 9 ''' 10 for I in enumerate (li ): 11 print (I) 12 returned results are not easy to understand: 13 (0, 'cell phone ') 14 (1, 'computer') 15 (2, 'mouse pad ') 16 (3, 'yacht ') 17''' 18 19 for key, value in enumerate (li): # set two values for receiving in the for loop, one for receiving key, A receiving value of 20 print (key, value) 21 ''' 22 prints the result: 23 0 mobile phone 24 1 computer 25 2 mouse pad 26 3 yacht 27 ''' 28 indium = input ("Enter serial number:") 29 print (li [int (indium)])
_ Author _ = 'prop 93 '#! /Usr/bin/env python #-*-coding: UTF-8-*-dic = {"Hebei": {"Shijiazhuang": ["Luquan", "Yuanshi ", "X City"], "Handan": ["Yongnian", "wexian", "cixian"]}, "Henan" :{}, "Shijiazhuang ": {} ret = [] for I in dic: print (I) # print all provinces prov = input ("Enter the province to query (the database is only Hebei ):") for I in dic [prov]: print (I) # print all city = input ("enter the city to query:") for I in dic [prov] [city]: print (I) # print all county towns = input ("Enter the queried County (currently no county data )") for I in dic [prov] [city] [town]: print (I)
1 _ author _ = 'prop 93 '2 #! /Usr/bin/env python 3 #-*-coding: UTF-8-*-4 5 # functional requirements: 6 #1. the user is required to enter total assets, for example, 2000 7 #2. the product list is displayed. You can select a product by serial number and add it to the shopping cart 8 #3. if the total amount of the purchased item is greater than the total assets, the system prompts that the account balance is insufficient. Otherwise, the purchase is successful. additional: can be recharged, a commodity removed from the shopping cart 10 11 goods = [12 {'name': 'pc', 'price': 1}, 13 {'name ': 'mouse ', 'price': 10}, 14 {'name': 'yacht', 'price': 100}, 15 {'name': 'beauty ', 'price': 1000}, 16 {'name': 'auto', 'price ': 10000} 17] 18 bag = [] # define empty shopping cart 19 shoppingcontinue = True # initialize whether to continue shopping to 20 fortune = Int (input ("enter total assets:") 21 for key, I in enumerate (goods): 22 print (key, I ['name']) # print the commodity list with the serial number 23 24 # enter the selected commodity and add it to the shopping cart loop 25 while shoppingcontinue: # while loop to determine whether to End shopping 26 p = int (input ("enter a serial number to select the product:") 27 bag. append (goods [indium]) 28 shoppingcontinue = bool (int (input ("enter 0 or 1 (0. end shopping 1. continue shopping): ") # use 0 or 1 entered by the user to determine whether to exit shopping 29 30 # comparison of the total price and assets, and whether to recharge the account: 31 money = 0 # total shopping price 32 actiontype = "0" # initialization operation type, 0 is recharge, 1 is to delete some items in the shopping cart 33 ifcharge = Fal Se # Whether to recharge 34 ifdel = False # whether to delete the cart product 35 for I in bag: 36 money + = I ['price'] # Add the total price of the purchased item to the price of the selected item 37 while money> fortune: 38 print ("the shopping balance is insufficient. Please recharge or delete the shopping cart item! ") 39 actiontype = input (" 0. recharge 1. delete some items in the shopping cart: ") 40 if (actiontype =" 0 "): 41 chargevalue = int (input (" Enter the Recharge Amount :")) 42 fortune + = chargevalue # total asset recharge 43 continue # determine whether the total shopping price is greater than the asset 44 elif actiontype = '1': 45 for key, I in enumerate (bag ): 46 print (key, I) 47 x = input ("Enter the product number you want to delete:") 48 money-= bag [int (x)] ['price'] # cost of deleting the item to be deleted 49 del bag [int (x)] # Delete the item 50 print (bag) from the shopping cart) # Re-print the shopping cart information 51 continue # re-determine whether the total shopping price is greater than the total assets 52 else: 53 con Tinue54 print ('shopping successful! The total cost is % d RMB. Welcome to the next visit! '% Money)