Python <---> 小課題練習作業,python---
作業一:
利用*字典*輸出目錄,可以選擇目錄進入,可以回退、退出!
#conding:utf8
menu = {'北京':{'昌平':{'沙河':{'昌平婦幼',}},'海澱':{'海澱一區':{'海澱二區'},}},} #字典嵌套
current_layer = menu #利用此變數來記錄當前的層的位置 pre_menu = [] #用【】來記錄每一層
while True: for k in current_layer: print (k) choice = input('>>>>>>>:').strip() #去除輸入選項連邊的空格 if len(choice) == 0: #通過len的長度來判斷使用者是否輸入了 continue
if choice in current_layer: pre_menu.append(current_layer) current_layer = current_layer[choice] elif choice == 'b': if len(pre_menu) > 0: #通過大於0 來判斷,還有目錄可退 current_layer = pre_menu.pop() #利用pop來彈出上面的一層。 elif choice == 'q': exit() |
作業二
list的小練習:實現購物車功能
product_list = [['iphone',6500],['Mobli',5000],['huawei',4000],['oppo',300],['vivo',100]]
money = int(input('please input your money:')) got_list = []
while True: for i,v in enumerate(product_list): #顯示二維的數列,0 ... 1.... 2.... print (i,v) choice_goods = input('which good do you want to get :')
if choice_goods.isdigit(): #判斷是否是輸入的數字 choice_goods = int(choice_goods) if choice_goods < len(product_list) and choice_goods >=0: #條件判斷,看是否超出範圍 if money > product_list[choice_goods][1]: #如果錢大於物品的價格 money -= product_list[choice_goods][1] #計算餘額 got_list.append(product_list[choice_goods][0]) #把買到的商品添加到列表中 print ('you good is %s' %(got_list)) print ('you left money is: %s' %(money)) print ('') print ('')
else: print ('you do not have enough money to bu anything!') print ('\n')
else: print ('we do not have this good') print ('\n') |
作業三
冒泡演算法小練習
a_list = [9,8,7,6,5,4,3,2,1]
for j in range(len(a_list) -1): #第二步,每次迴圈排好一個數,則需要len(a_list) -1 次,才能全部的排好 #第一步,初級演算法不減j,每次迴圈都是從頭到尾比較一遍,減去j,後面排好的則不用比較 for i in range(len(a_list)-1 - j): if a_list[i] > a_list[i+1]: #最大數排到最後面去 a_list[i],a_list[i+1] = a_list[i+1],a_list[i] #利用a,b = b,a 對調
print(a_list) |