#_ *_ Coding:utf-8 _*_
Import Sys,os,getpass
##################### #对raw_input输入字符类型判断并转化 #####################
def input_handle (s):
If Str.isdigit (s): # # #对输入是否是数字进行判断 # # #
s = Int (s) # # #如果输出的是个数字, then convert to integer type # # #
return s
################### #框架函数 ###################################
def framework (name= ", balance="):
Os.system (' cls ') # # # #清屏 # # #
balance = Int (balance)
Print ("'
******************************************************************
* *
* Welcome to Sky Shopping Platform *
* *
****************************************************************** *
Member:%s Current balance:%d
"% (name,balance))
####################### #商品列表展示函数 #######################
def shop_show (shop_dict):
Res_dict = {}
########### #对商品列表进行遍历并加上数字编号 ###############
i = 1
Print (' Product list: ')
Print (' ================================================================ ')
Print ('%-5s \ t%-20s \ t%-10s \ t%-10s '% (' number ', ' Product name ', ' commodity price (yuan) ', ' quantity of Goods (Units) '))
For K in Shop_dict:
v = shop_dict[k]
If Type (v) is dict:
Print ('%-5d \%-20s \ t%-10s \%-10s '% (i,k,v[' price '],v[' num '))
Res_dict[i] = [k,v[' price '],v[' num ']]
i + = 1
Print (' ================================================================ ')
Print (' Q:exit ')
Return res_dict
######################## #购物车函数 #############################
def shopping_cart (Shop_cart):
########### #对商品列表进行遍历并加上数字编号 ###############
Print (' ################## #欢迎来到您的购物车 ################## ')
Print ('%-20s \ t%-10s \%-10s \ t%-10s '% (' product name ', ' commodity price (yuan) ', ' Purchase quantity (per) ', ' Total purchase amount (yuan) '))
For K in Shop_cart:
v = shop_cart[k]
If Type (v) is list:
Print ('%-20s \%-10d \ t%-10d \%-10d '% (k,v[0],v[1],v[2]))
Print (' ################## #请确认您购买商品 #################### ')
############################### #主程序开始 ################################
Shopping_dict = {
' Iphone6 ': {' price ': 6000, ' num ': 10},
' ipad ': {' price ': +, ' num ': 20},
' Mi4 ': {' price ': $, ' num ': 43},
' Huawei6_plus ': {' price ': 1999, ' num ': 8},
}
i = 0
While I < 3: #只要用户登录异常不超过3次就不断循环
Name = Raw_input (' Please enter user name: ') #输入用户名
passwd = raw_input (' Please enter password: ') #输入隐藏密码
User_file = open (' Info.txt ', ' R ') #打开帐号文件
User_list = User_file.readlines ()
For User_line in User_list: #对帐号文件进行遍历
(user,password,balance) = User_line.strip (' \ n '). Split () #分别获取帐号, password information, and current balance
balance = Int (balance)
If name = = User and passwd = = Password: #如用户名和密码正常匹配
My_shop_cart = {}
First_flag = 1
While First_flag:
Framework (User,balance)
New_dict = Shop_show (shopping_dict)
Shop_index = raw_input (' Please enter product number | exit (q): ')
############## #如果输入非空, the input is judged and the type of conversion ###########
If Len (shop_index)! = 0:
Shop_index = Input_handle (Shop_index)
if Shop_index = = ' Q ': # # #如果输入为q, exit program # # #
Sys.exit (0)
Elif Shop_index in New_dict:
(shop_name,shop_price,shop_num) = (New_dict[shop_index][0], new_dict[shop_index][1], new_dict[shop_index][2])
Print (' Product information ' name:%-15s \ t Price:%-5d (yuan) \ t quantity:%-5d (Units) "'% (shop_name,shop_price,shop_num))
Second_flag = 1
While Second_flag:
Shop_num = raw_input (' Please enter the number of items to buy | back (b) | exit (q): ')
If Len (shop_num)! = 0:
Shop_num = Input_handle (shop_num)
if shop_num = = ' Q ': # # #如果输入为q, exit program # # #
Sys.exit (0)
elif Shop_num = = ' B ':
Break
Elif shop_num > 0 and Shop_num <= new_dict[shop_index][2]:
Shop_sum = Shop_price * Shop_num
If Shop_sum <= balance:
Print (' Total price of purchased product%s:%d '% (shop_name,shop_sum))
Add_flag = raw_input (' Please confirm whether to add to cart (y | n): ')
if Add_flag = = ' Y ':
My_shop_cart[shop_name] = [Shop_price,shop_num,shop_sum]
Balance-= Shop_sum
shopping_dict[shop_name][' num ']-= Shop_num
Second_flag = 0
Else
Break
Else
Print (' Your balance is insufficient, please recharge or re-select, thank you ')
Else
Pass
Shopping_cart (My_shop_cart)
Else
Pass
Else
If I! = 2: #i = 2 o'clock, is the last chance to have 0 more chances left without prompting
Print (' User or password error, please re-enter and%d chance '% (2-i))
i + = 1 #当用户输入错误时, increase the cycle value by 1
Else
Sys.exit (' User or password input error more than three times, exit the system, welcome next visit ') #用户输入三次错误后, abnormal exit
User_file.close () #关闭帐号文件
Python's writing shopping (next day)