For loop
For variable in range: code block ... contune #跳出本次循环接着执行下一次循环 for variable in range: code block ... break #跳出本层循环, go back to the previous for loop else: #其实for循环和while循环都有else子句, but the code block will be executed when the loop is fully executed ... Other main program code blocks ...
#作业1--jump out of layer three for follow Ring
for i in range (5): print ("I---", i) for j in range (5): Print ("J---", j) for k in range (5): if k == 2: print ("K---", k) break else: continue break else: continue ' Job 2-Shopping cart program customers can buy what they want based on their own salary, and the remaining amount will be calculated each time the purchase is completed '
Print ("----------welcome to oldboy market----------") print ("------today you can Buy all of these:-------") Print ("--iphone<>computer<>coffee<>book<> clothes--") Shopping_car = []client_salary = int (Input (" please input your Salary: ") while true: items = [[' Iphone ', 5000], [' Computer ', 7999], [' Coffee ', 30], [' book ', 80], [' Clother ', 600]] i = 1 enable_list = [] # What the user can buy disable_list = [] # The user cannot buy something cost_list = [] # price of goods to be purchased name_list = [] # can buy the name of the product print ("you can buy these:\n") for item in items: if item[1] <= client_salary: enable_list.append (item) cost_list.append (item[1]) name_list.append (item[0]) print (i, item[0], " ", item[1]) i += 1 else: disable_list.append (item) continue print ("q quit shopping") print ("------------------------------------------------------") print ("You can not buy these,because you don ' t have enough money!\n", disable_list) print ("------------------------------------------------------") choice = input ("What ' s your choice number or quit:" ) if choice == ' q ' or choice == ' Q ': &nBsp;print ("TODAY&NBSP;YOU&NBSP;HAVE&NBSP;BUY&NBSP;THESE&NBSP;:", Shopping_car) break else: choice = int (choice) -1 if choice <= len ( enable_list): if cost_list[choice] <= client_salary: shopping_car.append (Enable_list[choice]) # Add selected items to Cart client_salary = client_salary - cost_list[choice] print ("The " ,enable_list[choice] , " is added to the shoppingcart \nyour balance is ", Client_ Salary) print (" ------------------------------------------------------") print ("Have buy:", Shopping_car) print ("------------------------------------- -----------------") else: print ("You don ' t have enough money to buy this! ") continue else: &nBsp; print ("Your choice is wrong,please try again! ") continue
Python loop for loop and simulation shopping cart Applet