Python shopping cart and python shopping cart

Source: Internet
Author: User

Python shopping cart and python shopping cart

 


#-*-Coding: UTF-8 -*-
# Author: Mr Gan

Deficiency: The product ID must be an integer. If it is another type, an error is returned.
Import sys
Balance = int (input ("your money :"))
Shopping_bag = [] # create a shopping package
Shopping_list = [["iphone", 5800], ["watch", 2600], ["book", 56], ["computer", 7800]

While True:
For index, I in enumerate (shopping_list ):
Print (index, I)
Pro_num = input ("production number :")

With open ("balance file", 'W + ') as left_money:
If pro_num = "q ":
Left_money.write (balance)
Print ("your balance left \ 033 [31; 1 m % s \ 033 [0 m" % balance)
Sys. exit ()
Elif pro_num.isdigit (): # determines whether the input product number is a number.
Pro_num = int (pro_num)
# Pro_name = shopping_list [pro_num]

If 0 <= pro_num <len (shopping_list): # and pro_name [1] <= balance: # dynamic judgment, do not write it to death
Pro_name = shopping_list [pro_num]
If pro_name [1] <= balance:
Shopping_bag.append (shopping_list [pro_num] [0])
Balance-= pro_name [1]
Else:
Print ("your banlance is not ennough, please choose others ")
Else:
Print ("no production, choose again ")
For I in shopping_bag: # traverse purchased items
Print ("\ 033 [31; 1 m % s \ 033 [0 m" % I) # print all purchased items
Print ("your balance left \ 033 [31; 1 m % s \ 033 [0 m" % balance)
Pass

Related Article

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.