Client
Approximate process:
The buyer system first enters the buyer's balance, and then the balance in the file determines whether he spent the last time. If there is information, replace the balance of the buyer with the balance calculated in the file, if no information
Prove that the customer is the first time to enter the balance with the buyer. It then loops out the product subscript, and the product information. The customer chooses the product by subscript. The selected item will be added to the shopping cart list and then [1] to get the product's
Amount. Calculate the amount of the consumption, if the amount is greater than the balance then delete the item from the shopping cart list to tell the balance is insufficient, and prompt to buy how much money within the product. Each added item calculates the total amount of the cart
and compare it with the balance.
Finally, select "Q" to exit. The shopping cart list and balance will be saved in two files.
Product Information file content
[' Iphone ', 5800],[' Mac Pro ', 9800],[' Bike ', 800],[' Watch ', 10600],[' Coffee ', +], (' Pro ', 1899),,
Shopping Cart File contents
[' iphone ', 5800],[' iphone ', 5800],[' iphone ', 5800],,
Balance File Contents
17000
Official code:
f =Open' S1.txt ',' R ',encoding=' Utf-8 ')
First_line = F.readline ()
str2=first_line[0:-3]
b=Eval (str2)
#一个空列表 as a shopping cart
Shopping_cart=[]
#计算购物金额
money=0
Money2=0
money3=0
Pd=True
#客户输入工资 converted to int type
sallry=intInput"Please enter balance >>>")
#读取存有余额的文件
Sallry_txt =Open"Balance.txt","R")
Sallry_txt_line=sallry_txt.readline ()
sa=sallry_txt_line[0:]
#如果读取的是整数 put the value on the payroll.
If Sa.isdigit ():
sallry=Int (SA)
Print"You have previously logged in, the balance is:", sallry)
Else
#占位符什么都不做的意思
Pass
While PD:
#打印商品列表
For Id,itemInchEnumerate (b):
Print (id+1,item)
Id=Input' Please enter the selected product number >> ')
#如果输入q就退出
If id=="Q":
Break
# Otherwise if the input is an integer and greater than 0, less than the maximum number of the shopping list is added into the shopping cart
Elif Id.isdigit ()andInt (ID) >0andInt (ID) <Len (b) +1:
#加进购物车
Shopping_cart.append (b[Int (ID)-1])
#循环购物车
For IIn Shopping_cart:
Money + = i[1]
#如果刚加入的
If Money > Sallry:
Shopping_cart.pop ()
For I2In Shopping_cart:
Money2 + = i2[1]
Print"Consumption", money,"Yuan your balance is insufficient, please purchase amount not exceeding", Sallry-money2,"Of Goods")
Money2=0
money=0
Else
For i3In Shopping_cart:
Money3 + = i3[1]
Print"Bug>>", i3)
Sallry1 =sallry-money3
Print"Consumption", Money3,"Yuan, surplus", Sallry1,"Yuan")
money3=0
Money =0
#最终余额
Balance=sallry1
sallry1=20000
Else
Print"Input error, re-enter")
Print"The end of this shopping, welcome to the next visit")
#将购物清单写入
Files =Open ("Shopping_cart.txt","W") # "W"
Shopping_cart.append ("")
For I in Shopping_cart:
Files.write (str (i))
Files.write (",")
Files.close ()
#将余额写入
Files2 = Open ("Balance.txt","W")
Files2.write (str (Balance))
Files2.close ()
Python Learning notes--II Shopping Cart Applet _ client