Python實現的一個自動售飲料程式碼分享

來源:互聯網
上載者:User
寫這個程式的時候,我已學習Python將近有一百個小時,在CSDN上看到有人求助使用Python如何寫一個自動售飲料的程式,我一想,試試寫一個實用的售貨程式。當然,只是實現準系統,歡迎高手指點,新手學習參考。

運行環境:Python 2.7

# encoding=UTF-8loop=Truemoney=0while loop:    x = raw_input('提示:請投入金幣,結束投幣請按"q"鍵')    if x=='q':        if money==0:            print '錯誤:您未投入過紙幣,請至少投入一張紙幣後才能選購商品'        else:            print '提示:您已結束投幣,將進入購買商品操作介面'            loop = False    else:        try:            x = int(x)            money+=x            print '提示:您此次投幣',x,'元人民幣,您一共投幣',money,'元人民幣'        except Exception,e:            print '錯誤:您的金幣系統不識別,請重新投幣,謝謝!' GoodList = {    '可口可樂':2.5,    '果粒橙':3,    '奶茶':1.5,    '加多寶':4} i=0print '請選擇商品:'for x in GoodList:    i+=1    print '編號',i,'商品名稱',x,'價格',GoodList[x]print fanwei = range(len(GoodList))loop = Truewhile loop:    o = raw_input('提示:請輸入您要購買的商品編號,按"q"鍵結束購買')    if o=='q':        loop = False    else:        try:            o = int(o)            if o>=1 and o<=len(GoodList):                i=0                for x in GoodList:                    i+=1                    if i==o:                        if money>=GoodList[x]:                            money -= GoodList[x]                            print '提示:您購買的商品是:',x,',價格:',GoodList[x],',您還剩餘:',money,'元人民幣'                            if money==0:                                loop = False                        else:                            print '錯誤:您的餘額',money,'元已不足購買此商品',x,'[',GoodList[x],'元]'            else:                print '錯誤:您輸入的商品編號不存在,請重新輸入'        except Exception,e:            print '錯誤:請輸入正確的產品編號,謝謝合作!' if money>0:           print '提示:系統將找您,',money,'元人民幣,歡迎下次光臨'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.