Python 練習:簡單的購物車(二)

來源:互聯網
上載者:User

標籤:while   一個   app   ==   post   分享圖片   alt   mac   class   

最佳化了上一個購物車程式:http://www.cnblogs.com/klvchen/p/8577269.html

#輸入工資
salary = input("Please input your salary: ")

#判斷工資是否為整數if salary.isdigit(): salary = int(salary)else: exit("you must input digit")

#定義購物車cart = []#商品資訊msg = [["iphone6s", 5800], ["mac book", 9000], ["coffee", 32], ["bicycle", 1500]]while True:
  #展示商品資訊 for k, v in enumerate(msg, 1): print("%s 商品名稱: %s, 價格: %d" % (k, v[0], v[1])) choice = input("Please select the product number you need to purchase ! [q] to exit")
  #退出 if choice == ‘q‘: print("========== You have buy ==========") for l in cart: print(l) exit()
  #選擇商品 if choice.isdigit(): choice = int(choice) if 0 < choice <= len(msg):
       #判斷餘額是否充足 if salary >= msg[choice-1][1]: salary -= msg[choice-1][1] print("You have buy %s, money has %d" %(msg[choice-1][0], salary)) cart.append(msg[choice-1][0]) else: print("You don‘t you enough money! money has %d" %salary ) else: print("Please select right product number!") else: print("Please select right options")

運行結果如下:

程式還有很多可以最佳化的地方,以後有空再琢磨~

Python 練習:簡單的購物車(二)

聯繫我們

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