python日記,購物車程式

來源:互聯網
上載者:User

標籤:int   商品列表   bsp   logs   啟動   end   清單   else   python   

# readme:#     ne_zha#     blogger address:www.cnblogs.com/ne-zha#     要求:#       1.啟動程式,讓使用者輸入存款,然後列印商品列表#       2.允許使用者根據商品編號選擇購買商品#       3.使用者選擇完商品後檢測餘額是否足夠扣款,滿足提示扣款成功,不滿足提示餘額不足#       4.可隨時退出,退出時列印購買商品和餘額savings =input(‘請輸入您的存款:‘)commodity_list =[(‘iphone7‘,5888),(‘bike‘,1500),(‘huaweiP9‘,3666),(‘headset‘,199)]if savings.isdigit():    savings =int(savings)    while True:        for item in enumerate(commodity_list):            print(item)        user_choice =input(‘請輸入您想要的商品編號:‘)        if user_choice.isdigit():            user_choice =int(user_choice)            if user_choice < len(commodity_list) and user_choice >= 0:                choice = commodity_list[user_choice]                price_choice =choice[1]                if price_choice<=savings:                    buy_user =[]                    buy_user.append(choice[0])                    print(‘商品\033[1;31;40m%s\033[0m已加入購物車‘%choice[0])                    savings-=price_choice                    print(‘餘額:\033[1;31;40m%s\033[0m‘%savings)                else:                    print(‘餘額不足,購買失敗!‘)            else:                print(‘商品輸入有誤,請重新輸入:‘)        elif user_choice ==‘q‘:            print(‘您已買的商品清單與餘額:‘)            for i in buy_user:                print(‘\033[1;31;40mi\033[0m)                print(‘\033[1;31;40msavings\033[0m‘)                exit()        else:            print(‘invalid option‘)

 

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.