python的學習筆記/002-4(2018-5-20)

來源:互聯網
上載者:User

標籤:元組(tuple)的操作及購物車

元組及其操作
元組在很多地方與列表很像,都是有序的,都可以通過[]指定索引來擷取元素,不過元組建立好之後就不可以變動。

names=(‘sheldon‘,‘penny‘,‘leonard‘)print(names)print(names[1])  # 切print(names.count(‘penny‘))  # 統計print(names.index(‘penny‘))  # 索引# names[1]=‘潘妮‘     # 替換  不可以# names.remove(‘penny‘)   # 刪  不可以# names.append(‘潘妮‘)   # 增  不可以

購物車執行個體
1.簡單的購物車
存在的缺點一旦,商品號輸錯就好運行失敗;

salary=int(input(‘salary:‘))names1=(‘Iphone‘,‘Bike‘,‘Book‘,‘Car‘)names2=(‘2500‘,‘800‘,‘60‘,‘3000‘)names3=[]print(‘-------------------‘)for i in names1:    num=names1.index(i )    price=names2[names1.index(i) ]    print(num,i,price)print(‘-------------------‘)_abc=input(‘你是否要購物:(add)or(other)‘)while _abc==‘add‘:  num2=int(input("The shopping num:"))  price2=int(names2[num2])  if salary>price2:      names3.append(names1[num2])      names3.append(names2[num2])      print(‘你剛剛購買了:‘,num2,names1[num2],names2[num2 ])      salary=salary-price2      print(‘-------------------‘)      for i in names1:          num = names1.index(i)          price = names2[names1.index(i)]          print(num, i, price)      print(‘-------------------‘)      _abc=input(‘你是否要繼續購物:(add)or(other)‘)  else:      print("you money is not enougy")      breakprint("你購買的商品:",names3)print(‘你的餘額:‘,salary)

2.完善後的程式

product_list=[    (‘Iphone‘,5800),    (‘Mac pro‘,9800),    (‘Bike‘,8000),    (‘Book‘,20),    (‘Car‘,3000),    (‘Food‘,200)]                       # 巢狀型別shopping_list=[]salary=input(‘input you salary:‘)if salary.isdigit():    salary=int(salary)    while True:        for index,item in enumerate (product_list):  # 取出列表的下標            print(index,item)        use_choice=input("你要買什麼:")        if use_choice.isdigit() :            use_choice =int(use_choice )            if use_choice >=0 and use_choice <len(product_list ):                P_item=product_list [use_choice ]                if P_item[1]<=salary :                    shopping_list .append(P_item)                    salary -=P_item[1]                    print("Added %s into shopping car,you moneny is \033[31;1m%s\033[0m"%(P_item ,salary))                else:                    print("\033[41;1m你的餘額不足,只剩%s\033[0m"%(salary ))            else :                print("沒有該類產品...")        elif use_choice ==‘q‘:            print("----------shopping list-----------")            for i in shopping_list :                print(i)            print(‘你的餘額剩%s‘%(salary))            # break            exit()        else:            print("invalid option")

python的學習筆記/002-4(2018-5-20)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.