Python學習一之列表_練習_購物車__Python

來源:互聯網
上載者:User
列表練習之購物車代碼 涉及知識1 str.isdigist()判斷輸入字串是否為數字 涉及知識2 enumrate(list,index) ,從index開始添加下標 涉及知識3 while迴圈,for迴圈 涉及知識4 if else 涉及知識5 預留位置 涉及知識6 range和len 代碼如下
salary = input("請輸入您的工資>>:")saleList = [("tesla",900000),("pencl",20),("mac",12000),("ipX",9700),("ip5",1500),("ip7p",5000)]buyList=[]if salary.isdigit():    salary = int(salary)    while True :        for i ,v  in enumerate(saleList,1):            print(i,v)        choice = input("請選擇商品編碼,退出[quit:]>>:")        if choice.isdigit():            choice = int(choice)-1            if choice in range(len(saleList)):                sale_item = saleList[choice]                sale_item_price = sale_item[1]                if salary>=sale_item_price :                    print("您選擇商品:",sale_item[0])                    buyList.append(sale_item)                    salary-=sale_item_price                else :                    print("餘額不足,餘額s%元,商品單價為s%元"%(salary,sale_item_price))            else:                print("商品編號越界,請選擇正確的商品編號")        elif choice=="quit":            print("-----------------------您選擇的商品為----------------------")            for item in buyList:                print(item)            break        else:            print("輸入的商品編碼不正確")else:    print("請輸入正確的數字")

聯繫我們

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