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