Last night after night listening to a teacher to explain the list, and he has some ideas, shopping cart would be able to write, but there are still many mistakes, too many pit, to their own publicity department.
My Cart .py
sla = int (input ( "Please enter your salary:"))
Print ( '' '
. 1, ...... 5800 A
2, B ...... 12000
. 3, C ...... 31 is
. 4, D ...... 81
. 5, E ...... 800
'' ')
name = [[ "A", 5800], [ "B", 12000], [ "C", 31 is ], [ "D", 81], [ "E", 800]]
My = []
li = []
the while True:
k = the INPUT ( "do you want to continue to buy it select number or press?" q " exit ")
IF K ==" Q ":
BREAK
the else:
my.append (name [int (K) -1] [0])
li.append (name [int (K) -1] [. 1])
S = SUM (li)
IF S> SLA:
Print ( "you are out of balance, please choose a deleted:")
Print (My)
d = the iNPUT ( "Please enter the product number on the selected item, or press" q "exit")
if d=="q":
break
My del [int (D) -1]
del Li [int (D) -1]
S = SUM (Li)
Print ( "choose goods that you")
Print (My)
Print ( "Your balance is:" + str (sla-s))
The effect can be achieved, among many places choose the wrong time, directly on the error, but did not take into account:
1: If the wage user input is a string of letters like, how do?
2, when the purchase of goods entered by the user is a string of letters like it?
3, when the purchase of goods entered by the user is greater than the number of types of goods it?
The following is a teacher code:
Teacher cart .py
product_list = [
( "iPhone", 5000),
( "MAC Pro", 9800),
( "Bike", 800),
( "Watch", 10600),
( "Coffer", 31 is),
( "Cup", 120 )
] # tuple contents to not change inside
the shopping_list = []
the salary = iNPUT ( "iNPUT you the salary:")
iF salary.isdigit (): # isidgit string () method can be used to determine whether the input is integer, so, the condition to true
the salary = int (the salary) converted to integer #
the while True:
for K, I in the enumerate (product_list):
#priint (product_list.index (I), I), if for use in a argument, consider using this method, index () query can get the position of the element
Print (k, i)
USER_CHOICE the iNPUT = ( "enter a product you want:")
IF user_choice.isdigit ():# Judge again the user input is not an integer
USER_CHOICE = int (USER_CHOICE)
IF USER_CHOICE <len (product_list) and USER_CHOICE> = 0: integer input by the user # judgment is not in the list length, len () method to get the length
p_item = product_list [user_choice] # within the range then the selected elements (ie, the tuple) taken out
if [1] <= salary p_item : # wages and commodity prices, compare the size of
shopping_list.append (p_item) # shopping cart Add this product
salary- = p_item [1] # salary minus the price of the commodity
print ( "S INTO Shopping join%, and you Current iS% S"% (p_item, salary))
the else:
print ( "your balance reaches% s, bought an ass." the salary%)
the else:
Print ( "product not found")
elif USER_CHOICE == "Q":
Print ( "-------shopping list------")
P in the shopping_list for:
Print (P)
Print ( "% S IS your Current Balance" the salary%)
Exit ()
the else:
Print ( "you entered is incorrect")
'' '
the enumerate Usage:
A = [1,2, . 3]
for the enumerate in I (A):
Print (I)
>>>
(0,1)
(1,2)
(2,3)
'' '