Python3 Shopping Cart applet, balance write file Save

Source: Internet
Author: User
Tags macbook

Python3 Shopping Cart applet, balance write file Save

#!/usr/bin/env python#-*-coding:utf-8-*-# Author:hiuhung wangoods = (("MiNote3", 2499), ("Bike", 799), ("MACB    Ook ", 6999), (" Coffee ", +), (" RedMiNote3 ", 1099), (" Python 3 ", ()) def main ():" Entry: Return: "    # Create a file to store the balance. Try:with open (' Balance.txt ', ' R ') as F:data_str = F.read () if Data_str and (not data_str.is Space ()): # has content, balance = Int (data_str) Else: # There is a file, but no content balance = input ("please                    Enter your wallet balance: ") if Balance.isnumeric (): balance = Int (balance) Else:                    Print ("Please enter a positive integer") exit () with open (' Balance.txt ', ' W ') as F:  F.write (str (balance)) except Filenotfounderror as E: # Create and write content without this file balance = input ("Please enter your wallet balance:") if Balance.isnumeric (): balance = Int (balance) else:print ("Please enter a positive integer") exit () W ItH Open (' Balance.txt ', ' W ') as F:f.write (str (balance)) Finally:f.close () # closes the file. Go_back_flag = True Shopping_list = [] While go_back_flag:for I, j in Enumerate (goods): print (I, j) User_chiose = input ("Wallet balance is:%. 2f, what do you want to buy?") "% (balance)) if User_chiose = = (" q "or" Q "): Go_back_flag = False Continue elif user _chiose.isnumeric (): user_chiose = Int (user_chiose) else:print ("Please enter the above serial number") Contin                UE if User_chiose <= len (Goods)-1: #符合 if goods[user_chiose][1] <= balance: #买得起 Balance-= goods[user_chiose][1] with open (' Balance.txt ', ' W ') as F: # Update balance (overwrite) F.WR                ITE (str (balance)) F.close () print ("%s added to your cart"% (goods[user_chiose][0)) Shopping_list.append (Goods[user_chiose]) else:print ("The balance is not enough to buy.     ") Else:       Print ("Out of range, no this serial number") print ("Your wallet balance is:%.2f.")  "% (balance)) If Len (shopping_list) = = 0:print (" You don't have to buy anything ") Else:print (" The product you have purchased below ") for I In Shopping_list:print (i) if __name__ = = "__main__": Main ()

  

The effect is as follows:

C:\Python36\python.exe d:/py/1704/day05/shopping cart. Py Please enter your wallet balance: 66660 (' MiNote3 ', 2499) 1 (' Bike ', 799) 2 (' MacBook ', 6999) 3 (' Coffee ', 4 (' RedMiNote3 ', 1099) 5 (' Python 3 ', 59) wallet balance is: 6666.00, what do you want to buy? 3 added Coffee to your cart 0 (' MiNote3 ', 2499) 1 (' Bike ', ' 799 ') 2 (' MacBook ', 6999) 3 (' Coffee ', ' 4 ') + (' RedMiNote3 ', 1099) 5 (' Python 3 ') , 59) Wallet balance is: 6641.00, what do you want to buy? 2 The balance is not enough to buy. 0 (' MiNote3 ', 2499) 1 (' Bike ', 799) 2 (' MacBook ', ' 6999 ') 3 (' Coffee ', ' + 4 ') 5 (' RedMiNote3 ', ' 1099 ') (' Python 3 ', 59) wallet balance is: 6641.0 0, what do you want to buy? 1 added Bike to your cart 0 (' MiNote3 ', 2499) 1 (' Bike ', 799) 2 (' MacBook ', ' 6999 ') 3 (' Coffee ', ' 4 ') + (' RedMiNote3 ', 1099) 5 (' Python 3 ', 59) Wallet balance is: 5842.00, what do you want to buy? Q Your wallet balance is: 5842.00. Below will list the items you have purchased (' Coffee ', ' + ') (' Bike ', 799) Process finished with exit code 0

  

Python3 Shopping Cart applet, balance write file Save

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.