python學習 day2購物車程式

來源:互聯網
上載者:User

標籤:item   for   enum   day   bsp   下一步   idt   cti   exit   

production = [    ["Apple",7000],    ["watch",3000],    ["pad",3500],    ["book",100],]shopping_list =[]salary = input("Your salary:")if salary.isdigit():    salary = int(salary)    while True:        for index,item in enumerate(production):            #print(production.index(item),item)            print(index,item)        choice = input("你想買什嗎?")        if choice.isdigit():            choice = int(choice)            if choice < len(production) and choice >= 0:                if production[choice][1] <= salary:                    salary -= production[choice][1]                    shopping_list.append(production[choice])                    print("你已經購買了%s,還剩下\033[41;1m%s\033[0m"%(production[choice][0],salary))                else:                    print("你的錢不夠")            else:                print("你要購買的商品不存在")        elif choice =="q":            print("--------shopping list----------")            for p in shopping_list:                print(p)            print("你的錢還剩下",salary)            exit()        else:            print("請重新輸入商品編碼")else:    print("請重新輸入")

 

備忘:注意for迴圈的使用,比如第一個for迴圈處,print完下一步choice應該與for處於同一層次,才能列印出完整的商品列表,否則將會如下

Your salary:90000 ‘apple‘ 7000ni xianng mai shen me?0.....1 ‘watch‘ 3000...

在編寫的過程中要注意思路清晰,一步一步來,可以在紙上列下流程圖,多加練習。

殺人須就咽喉著刀,吾輩為學,當從心髓入微處用力,自然篤實光輝。

python學習 day2購物車程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.