#!/usr/bin/env python#-*-coding:utf-8-*-# products = {"IPhone X": 8999, "Xiaomi Notebook": 5599, "Xiaomi Smart home Kit": 299, "Adidas Sneakers": 566} # with words Code to do the outermost, the dictionary is unordered # products = [{"IPhone X": 8999},{"Xiaomi Notebook": 5599},{"Xiaomi smart home kit": 299},{"Adidas sneakers": 566}] # do not use a dictionary as the inner layer, The loop is not easy to get keys and values of products = [["IPhone X", 8999],["Xiaomi Notebook", 5599],["Xiaomi Smart home kit", 299],["Adidas sneakers", 566] # both inside and outside are used list, with index convenient to value Shopping_ Carts = [] #初始购物车为空money = 0 # The initial wage is the empty money + = Int (input ("Please enter your Salary:")) Exit_flag = Falsewhile not exit_flag:print ("\ n---- --------Product List-----------\ n ") # Commodity list for index, product in enumerate (products): print ("%s.%s%d "% (index, Produc T[0], product[1]) print ("\ n-------------------------------\ n") product_id = input ("Please enter the product number you want to buy (enter Q to exit):") if PR Oduct_id.isdigit (): product_id = Int (product_id) if product_id >= 0 and product_id < Len (products): If Money >= products[product_id][1]: money-= products[product_id][1] Shopping_c Arts.append (Products[product_id][0]) Else:print ("\ n your salary balance is%s yuan, and the value of the item is%s, cannot be purchased! \ n "% (Money,products[product_id][1])) # Exit_flag =true else:print (" Wrong product designator, please re-enter ") E Lif product_id = = "Q": If Len (shopping_carts) > 0:print ("\ nthe items you added to the cart are as follows: \ n") for index, p Roduct_carts in Enumerate (shopping_carts): Print ("%s.%s"% (index, product_carts)) Else: Print ("\ n your shopping cart is empty!") ") print (" \ n your salary balance is%s yuan! ") \ n "% (money)) Exit_flag = True
#!/usr/bin/env python#-*-Coding:utf-8-*-china = {' Henan province ': {' Jiaozuo ': [' Wuzhi ', ' Wen County ', ' fraternity '], ' Zhengzhou ': [' Xinzheng ', ' Ying ' Yang ', ' Zhongmu ', ' Kaifeng ': [' Lankao ', ' Weishi ', ' Qixian '],}, ' Guangdong Province ': {' Guangzhou ': [' Yuexiu ', ' Liwan ', ' Panyu '], ' Shenzhen ': [' Futian ', ' Lo Wu ' , ' Longgang ', ' Dongguan ': [' Guancheng ', ' Nancheng ', ' Dongcheng '],},}exit_flag = Falsewhile not exit_flag: # 1th Loop print ("---------------- -----------------") Print (" Welcome to China ") print ("---------------------------------") for province in Ch Ina:print (province) print () Choice_province = input ("1. Enter the province you want to go to (Enter Q exit, enter B to return to the superior):") print () if Choic E_province = = "Q": Exit_flag = True elif choice_province = = "B": Continue # Jump out of the remaining statement of the 1th Loop and proceed to the next loop (select province) Elif choice_province in China:while not exit_flag: # 2nd loop for City in China[choice_province]: Print (city) print () choice_city = input ("2. Please enter the urban area you want to go to (Enter Q exit, enter B to return to the superior):") pri NT () if ChoicE_city = = "Q": Exit_flag = True elif choice_city = = "B": Break # jumps out of the 2nd loop, re- 1-Layer Loop (select province) elif choice_city in china[choice_province]: And not exit_flag: # 3rd Loop For county in china[choice_province][choice_city]: print (county) print () Choice_county = Input ("3." Enter the county you want to go to (Enter Q exit, enter B to return to the superior): ") print () I F choice_county = = "Q": Exit_flag = True elif Choice_county = = "B": Break # jump out of the entire 3rd loop, re-cycle 2nd (select downtown) elif Choice_county in china[choice_province][choice_city] : Print ("---------------------------------") print ("Your present position is:", Choice_provin CE, choice_city, choice_county) print ("---------------------------------") E Xit_flag = True Else:print ("The county you entered does not exist") print () continue Else:print ("The city you entered does not exist") print () Continue Else:print ("You Lose Not present in the province ") print () continue
2nd Python Basics-character encoding & data type Shopping cart & Multilevel Menu Jobs