python學習day04

來源:互聯網
上載者:User

標籤:rod   --   列表   pre   style   AC   int   操作   顯示   

一:今天是一個學習列表後的實踐訓練

購物小程式:

#codeing:UTF-8#__author__:Duke#date:2018/3/1/001product_list = [    (‘mac‘,7000),    (‘bike‘,1000),    (‘phone‘,2000),    (‘kindle‘,800),    (‘iwatch‘,3000),];shopping_car = [];saving = input("please input your money:")  #saving  為消費者的存款if saving.isdigit():    saving = int(saving);    while True:        #列印商品列表        for i, v in enumerate (product_list, 1):            print (i, v[0], v[1]);        #選擇購買商品的編號        choice = input(‘請輸入購買商品的編號[退出:q ]:‘)        #驗證使用者的輸入        if choice.isdigit():            choice = int(choice)            if choice>0 and choice <= len(product_list) :                p_item = product_list[choice-1];       #挑出選擇的商品                if p_item[1] < saving:                 #判斷餘額是否夠買此商品                    saving -= p_item[1];               #付賬                    shopping_car.append(p_item);       #將商品加入訂單                    print("你購買 %s 成功 " % p_item[0]);                else:                    print("你的餘額不足,還剩 %s" % saving)    #餘額不足的提示            else:                print(‘你的商品編號不存在...‘);            #選擇的商品不存在時        elif choice == ‘q‘:            print("----------你購買的商品如下-------");   #退出下單操作            for i,v in enumerate (shopping_car,1) :      #顯示已購商品                print(i,v)            print ("你的餘額為: %s" % saving)           #顯示餘額            break;        else:            print(‘invalid input...‘);              #錯誤輸入提示

有不明白的地方歡迎留言。

python學習day04

聯繫我們

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