Python basics-small program exercises, python basic applets
1. directly jump out of all loops from the 3rd-layer Loop
Break_flag = Falsecount = 0 while break_flag = False: print ("-first layer") while break_flag = False: print ("second layer") while break_flag = False: count + = 1 if count> 10: break_flag = True print ("Layer 3") print ("keep going... ")With break_flag
# Break_flag = Falsecount = 0 while count <3: print ("-first layer") while count <3: print ("second layer") while count <= 10: count + = 1 # if count> 10: # break_flag = True print ("Layer 3") print ("keep going... ")Without break_flag
2. Shopping Cart program
Goods_list = [['iphone7', 5800], ['coffee ', 30], ['book', 99], ['bike', 199], ['vivi x9', 2499] # product list shopping_cart = [] # user shopping cart list salary = int (input ('input your salary :')) # user salary m = salaryk = 0 while True: index = 0 for goods in goods_list: # print the product list print (index, goods) index + = 1 choice = input ('>>> :'). strip () if choice. isdigit (): choice = int (choice) if choice> = 0 and choice <len (goods_list): goods = goods_li St [choice] if goods [1] <= salary: # determine whether the user has enough money to pay for the selected item shopping_cart.append (goods) salary-= goods [1] print ('add goods '+ str (goods [0]) + 'into shopping cart! Your current balance: '+ str (salary) else: print ('no enough money! The price is: '+ str (goods [1]) + '! Need more: '+ str (goods [1]-salary) else: print ('have no this goods! ') Elif choice = 'q': print (' ---------- Your shopping cart ---------- ') print ('Id goods quantity price total') for I in range (len (goods_list )): j = shopping_cart.count (goods_list [I]) if j> 0: k + = 1 print (k, '\ t', goods_list [I] [0], '\ t', j,' \ t \ t', goods_list [I] [1], '\ t', j * goods_list [I] [1]) print ('total price is: ', m-salary) break else: print ('have no this goods ')Shopping Cart program
Goods_list = [['iphone7', 5800], ['coffee ', 30], ['book', 99], ['bike', 199], ['vivi x9', 2499] # product list shopping_cart ={}# the user's shopping cart list changes from list to dictionary salary = int (input ('input your salary :')) # user salary # m = salary # do not need this item # k = 0 # print ID of the user's shopping cart item, move the position outside the print loop while True: index = 0 for goods in goods_list: # print the product list print (index, goods) index + = 1 choice = input ('>>> :'). strip () if choice. isdigit (): # determine whether it is a number choice = int (choice) if c Hoice> = 0 and choice <len (goods_list): # goods exist goods = goods_list [choice] if goods [1] <= salary: # determine if the user has enough money to pay for the selected item if goods [0] in shopping_cart: # previously purchased shopping_cart [goods [0] [1] + = 1 # shopping Quantity Plus 1 else: shopping_cart [goods [0] = [goods [1], 1] # create a purchase record for a new item salary-= goods [1] # deduction print ('add goods '+ str (goods [0]) + 'into shopping cart! Your current balance: '+ str (salary) else: print ('no enough money! The price is: '+ str (goods [1]) + '! Need more: '+ str (goods [1]-salary) else: print ('have no this goods! ') Elif choice = 'q': id_counter = 1 # initialize the item ID total_cost = 0 # initialize the total cost of the item print (' ---------- Your shopping cart ----------') print ('Id goods quantity price total') for I in shopping_cart: print ("%-5 s %-10 s %-12 s %-10 s %-s" % (id_counter, I, shopping_cart [I] [1], shopping_cart [I] [0], shopping_cart [I] [0] * shopping_cart [I] [1]) id_counter + = 1 # item ID plus 1 total_cost + = shopping_cart [I] [0] * shopping_cart [I] [1] # print ('total price is: ', total_cost) break else: print ('have no this goods ')Shopping Cart program optimization, using dictionaries