This is the fourth day of my participation in the August Text Challenge.More challenges in August
Title 1:
[‘royal’,20,[2000,3,18]], take the name, age, year, month and day of birth from the list and assign them to different variables
L = [' royal ', 20, 2000,3,18 []] name = l [0] during the age = l [1] [2] = l [0] month = l [2] [1] day = l [2] [2]Copy the code
Topic 2:
Simulate queues with list INSERT and POP methods
Queue: FIFO, first in, first out
L = [] # team L.A. ppend (' one ') L.A. ppend (' two ') L.A. ppend (' three ') print (l) # out l.p op (0) l.p op (0) l.p op (0)Copy the code
Title 3:
Stack is simulated with list insert and POP methods
Stack: LIFO, last in first out
L =[] # print(l.pop()) print(l.pop()) print(l.pop()) print(l.pop())Copy the code
Topic 4:
Simple shopping cart, requirements are as follows:
To print the detailed information of goods, the user enters the name of goods and the number of purchases, and then adds the name, price and number of purchases to the shopping list in the form of triples. Msg_dic ={' Apple ':10, 'Tesla ':100000,' MAC ':3000, 'Lenovo ':30000, 'chicken':10,}Copy the code
Code:
god_list = [] flag = True while flag: God_num = input(' Please input number of purchases: ') if god_price.isdigit() and god_num.isdigit(): flag = False god_price = int(god_price) god_num = int(god_num) god = (god_name,god_price,god_num) god_list.append(god) Print (shop_list) else: print(" You typed wrong, please retype ")Copy the code
Answer:
1. First of all, let’s analyze the variables and set a list. Shop_list is used as a shopping list to store the product information entered by users. Here we set the name of the product entered by the user to god_name, the unit price to god_price, and the number of purchases to god_num.
God_list = [] god_name = input(' please input commodity name: ') GOD_price = input(' please input price: ')Copy the code
2. Next we need to determine the price and number of strings that need to be pure digits, so we add a condition, if the string is pure digits to int.
if god_price.isdigit() and god_num.isdigit():
god_price = int(god_price)
god_num = int(god_num)
Copy the code
3. Place it in a tuple, place the tuple in the list of goods, and output it
if god_price.isdigit() and god_num.isdigit():
god_price = int(god_price)
god_num = int(god_num)
god = (god_name,god_price,god_num)
god_list.append(god)
print(god_list)
Copy the code
4. However, if the price and quantity entered by the user are not pure numeric strings, they must be re-entered
Else: print(' Your input is wrong, please retype ')Copy the code
5. It is unknown whether the user input is wrong or not, so it needs to judge whether to repeat the execution according to the conditions, so we set a while loop to judge, we set a variable flag, the default is True, that is, the loop is entered by default, if the user input is correct, the flag is changed to False to end the loop, otherwise the flag is not changed.
god_list = [] flag = True while flag: God_num = input(' Please input number of purchases: ') if god_price.isdigit() and god_num.isdigit(): flag = False god_price = int(god_price) god_num = int(god_num) god = (god_name,god_price,god_num) god_list.append(god) Print (god_list) else: print(' Your input is wrong, please reenter ')Copy the code
Title 5:
Has the following values set,22,33,44,55,66,77,88,99 [11], all will be greater than 66 to keep the value of the dictionary is the first key, to save less than the value of 66 to the second key values
L = d = {} k2,22,33,44,55,66,77,88,99 [11] = [] for v l: in the if v > 66: d [' k1] = v else: k2.append(v) d['k2'] = k2 print(d)Copy the code
Answer:
1. First we need a list containing these numbers, and create an empty dictionary, and then create an empty list for the second key
L =,22,33,44,55,66,77,88,99 [11] d = {} k2 = []Copy the code
2. Then iterate through the values in the L list through the for loop
for v in l:
Copy the code
3. Then determine whether the v is greater than 66. If it is greater than 66, save it in K1, and add it to the k2 list if it is less than 66
if v > 66:
d['k1'] = v
else:
k2.append(v)
d['k2'] = k2
Copy the code
4. Output the dictionary
L = d = {} k2,22,33,44,55,66,77,88,99 [11] = [] for v l: in the if v > 66: d [' k1] = v else: k2.append(v) d['k2'] = k2 print(d)Copy the code
Topic 6:
Count the number of words in s=’ Hello world royal say hello world’
s='hello world r0ya1 say hello world world'
hello_count = s.count('hello')
world_count = s.count('world')
r0ya1_count = s.count('r0ya1')
say_count = s.count('say')
print('''hello:{hello_count}\n======
world:{world_count}\n======
r0ya1:{r0ya1_count}\n======
say:{say_count}\n======
'''.format(hello_count = hello_count,world_count = world_count , r0ya1_count = r0ya1_count , say_count = say_count))
Copy the code
Answer:
1. The string function count is used to count each word, and then pass it to the corresponding variable
s='hello world r0ya1 say hello world world'
hello_count = s.count('hello')
world_count = s.count('world')
r0ya1_count = s.count('r0ya1')
say_count = s.count('say')
Copy the code
2. Format the output as usual
print('''hello:{hello_count}\n======
world:{world_count}\n======
r0ya1:{r0ya1_count}\n======
say:{say_count}\n======
'''.format(hello_count = hello_count,world_count = world_count , r0ya1_count = r0ya1_count , say_count = say_count))
Copy the code
\