Programming the basis of simple python cart

#good folder contents 
[
[ 'Iphone7', 5800],
[ 'Coffee', 30],
[ 'Geda', 10],
[ 'the Python Book', 99],
[ 'Bike', 199],
[ ' X9 ViVo ', 2499],
]






user = {'username': None}
shopping_car_dict = dict()
money = [0]


def get_goods():
with open('goods.txt','r',encoding='utf8') as fr:
goods_list = fr.read()
goods_list = eval(goods_list)

return goods_list


goods_list = get_goods()


def register():
print('请注册')

if not user['username']:

username = input('username:')
pwd = input('pwd:')


with open('user_info.txt','a',encoding='utf8') as fa:
fa.write(f'{username}:{pwd} |')
else:
print('已登录,无法再注册')


def login(): IF not the User [ 'username']:
print ( 'Welcome')



INPUT = username ( 'username:')
pwd = INPUT ( 'pwd:')


inp_user_info = F '{username}: {} pwd'

with Open ( 'user_info.txt', 'R & lt', encoding = 'UTF8') AS fr:
user_info = fr.read ()
user_info_split = user_info.split ( '|')

IF inp_user_info in user_info_split:
Print ( 'successful landing')
the User [ 'username'] = username
the else:
Print ( 'account or password input error' )

the else:
Print ( 'You've landed')


DEF Shopping ():
Print ( 'Welcome to the shopping malls')

the while True:

for IND, Goods in the enumerate (goods_list):

print (f 'Product Number: {ind}', goods)


goods_choice = input ( 'Please enter the article you want, (Enter q to quit):')

IF goods_choice == 'q':
BREAK

goods_choice = int (goods_choice)
Goods = goods_list [goods_choice]
Print (f 'Shopping Success: {goods } \ n-')


goods_name = goods [0]
IF goods_name in shopping_car_dict:
shopping_car_dict [goods_name] + =. 1
the else:
shopping_car_dict [goods_name] =. 1


Money [0] + = goods [. 1]

Print (F' cart: { shopping_car_dict}, total: {money [0]} ')


DEF shopping_car ():
Print (' shopping cart ')

the while True:
Print (F' cart: {shopping_car_dict}, total: {money [0]} ')

goods_choice = input ( 'Enter unnecessary goods, if not press exit q')

IF goods_choice == 'q':
BREAK

shopping_car_dict [goods_choice] - =. 1

for Goods in goods_list:

IF goods_choice in Goods:

Money [0] - = goods [. 1]



DEF pay ():
Print ( 'request payment')

Print (F 'cart: {shopping_car_dict}, total: Money {[0]}')

Choice = INPUT ( 'whether to pay (Y / y), if the payment (N / n), the payment will not empty cart: ')


IF Choice ==' the Y 'or Choice ==' Y ':
Print (F' pay {money [0]} successful, purchased shopping_car_dict} { ')
elif Choice ==' N 'or Choice ==' n-':
shopping_car_dict.clear ()
Money [0] = 0
Print ('Unpaid, empty shopping cart success')

the else:
print ( 'input is not valid')

func_msg = '' '
. 1: Register
2: Log
3: cart
4: Shopping Cart
5: checkout
q: End
' ''


func_dict = {
'. 1': Register,
'2': Login,
'. 3': Shopping,
'. 4': shopping_car,
'. 5': Pay,
}

the while True:
Print (func_msg)
func_choice = INPUT ( 'select you need')
IF func_choice == 'Q':
BREAK
func_dict [ func_choice] ()

Guess you like

Origin www.cnblogs.com/zfb123-/p/11354150.html