Noodle cart

Keywords: PHP encoding

1. noodle cart

    1. Print out the goods in cycle first
    2. The user inputs the number to select the product (judge whether it is a number and whether the number is within the range)
    3 take out the commodity name and price
    4. Judge whether the user balance is greater than the commodity price
    5. If the balance is greater than the commodity price, judge whether the commodity is in the shopping cart.
        5.1 in the shopping cart, number plus 1
        5.1 not in the shopping cart, spell out a dictionary and put it in.
    6 user balance minus commodity price
    7 cost plus commodity price
    8 when you enter q, buy the product
        8.1 consumption is 0, exit directly
        8.2 print shopping cart
        8.3 accept user input, whether to buy when input y, directly adjust the shopping interface to realize shopping
user_info = {
    'user': None,
    'pwd': None,
    'balance': None
}

import os

# register
while True:
    user = input('user:')
    if os.path.exists(f'{user}.txt'):
        print('User name already exists!')
        continue

    pwd = input('pwd:')
    re_pwd = input('pwd:')
    if pwd == re_pwd:
        user_info = ','.join([user, pwd, '10000'])
        with open(f'{user}.txt', 'w', encoding='utf-8') as f:
            f.write(user_info)
            f.flush()
            print('login was successful!')
            break
    else:
        print('The two passwords are inconsistent.')


# Sign in
while True:
    user = input('user:')
    if not os.path.exists(f'{user}.txt'):
        print('user name does not exist!')
        continue
    with open(f'{user}.txt', 'r', encoding='utf-8') as f:
        user_content = f.read()
        user, file_pwd, balance = user_content.split(',')

    pwd = input('pwd:')

    if pwd == file_pwd:
        user_info['user'] = user
        user_info['pwd'] = file_pwd
        user_info['balance'] = int(balance)
        print('Login successfully!')
        break
    else:
        print('Password error')

'''
1 Print out the goods in cycle first
2 User input number to select goods (judge whether it is a number, and judge whether the input number is within the range)
3 Take out the commodity name and price
4 Judge whether the user balance is greater than the commodity price
5 If the balance is greater than the commodity price, judge whether the commodity is in the shopping cart.
    5.1 In the shopping cart, number plus one
    5.1 Not in the shopping cart, spell out a dictionary and put it in({'good': {'price': 10,'count': 1}})
6 User balance minus commodity price
7 Cost plus commodity price
8 When input q When buying goods
    8.1 Consumption is 0, exit directly
    8.2 Print cart
    8.3 Accept user input, purchase current input or not y,Directly adjust the shopping interface to realize shopping
'''


# Shopping Cart
good_list = [
    ['Guangdong chicken feet', 20],
    ['T-shirt', 150],
    ['AJ1', 2000],
    ['macbook pro', 18888],
]

shopping_cart = {}

cost = 0

while True:

    if cost:
        print(shopping_cart, f'The total price of your shopping cart is{cost}')
        sure = input('Please confirm the settlement: input y Confirm the purchase, otherwise exit the program!').strip()
        if sure == 'y':
            user_info['balance'] -= cost
            with open(f'{user_info["user"]}.txt', 'w', encoding='utf-8') as f:
                user_info['balance'] = str(user_info['balance'])
                res = ','.join([user_info['user'], user_info['pwd'], user_info['balance']])
                f.write(res)
                f.flush()
                print('Purchase success, looking forward to your next visit!')
                break
        else:
            cost = 0
            print('Exit procedure!')
            break


    for index, goods in enumerate(good_list):
        print(index, goods)

    choice = input('Please select a product number:').strip()

    if not choice.isdigit():
        print('Incorrect input,please enter a number')
        continue
    choice = int(choice)
    good, price = good_list[choice]
    print(good, price)

    if user_info['balance'] >= price:
        if good not in shopping_cart:
            shopping_cart[good] = 1
        else:
            shopping_cart[good] += 1

        cost += price
        print('Add shopping cart successfully!')

 

import  os
goods_list = [['apple',1000],['cucumber',500],['orange',1000],['chili',100000]]
goods_dict = {}
cost = 0
balance = None
while True:
    print("""
    1  register
    2  Sign in
    3  Shopping
    """)
    choice = input('Please enter the number:')
    if choice == '1':
        while True:
            print('Registration function:')
            name = input('Please enter your account name:').strip()
            if not os.path.exists('{}.txt'.format(name)):  #Both upper and lower case can be used???
                pwd = input('Please enter your password:').strip()
                re_user_pwd = input('Please confirm your password:').strip()
                if pwd != re_user_pwd:
                    print('The two passwords are inconsistent!')
                    break
                else:
                    res = '|'.join([name,pwd,'10000'])
                    with open(r'{}.txt'.format(name),'w',encoding='utf-8') as f :
                        f.write(res)
                        f.flush()
                        print(f'Congratulations{name}Registration complete!')
                        break
            else:
                print('User already exists!')
                break
        # print(f'Congratulations{user_name}Registration complete!')

    elif choice == '2':
        while True:
            print('Login function:')
            name = input('Please enter your account:')
            if not os.path.exists('{}.txt'.format(name)):
                print('User does not exist!')
                break
            else:
                with open(r'{}.txt'.format(name),'r',encoding='utf-8') as f1 :
                    db_name,db_pwd,balance = f1.read().split('|')
                    # print(balance)
                pwd = input('Please input a password:')
                if name == db_name and pwd == db_pwd :
                    print('Login succeeded! Welcome{}'.format(name))
                    print('You still have',balance)
                    break
                else:
                    print('Your account and password don't match!')
                    break
    elif choice == '3':
        if  balance: #If you don't log in, the shopping balance will be found and an error will be reported. First, define an empty balance in the global.
                     # Balance reassignment after login no None,You can shop normally. If you don't log in, the balance is None Unable to enter
            balance = int(balance)
            while True :


                for index,goods_ifo in enumerate(goods_list) :
                    print(index,goods_ifo)
                good_num = input('Please enter the product number:')
                if not good_num.isdigit()  or int(good_num) not in range(0,len(goods_list)):
                    print('There's nothing you want')
                    continue
                #good = goods_list[int(good_num)][0]
                # price = goods_list[int(good_num)][1]
                good,price = goods_list[int(good_num)]  #Just decompress and assign directly. There is no need to write two steps.
                if balance < price:
                    print('Sorry, your credit is running low!')
                    continue
                if good not in goods_dict:
                    goods_dict[good] = {'price': price, 'count': 1}
                else:
                    goods_dict[good]['count'] += 1
                print(goods_dict)
                balance -= int(price)
                cost += price
                choice = input('Please enter for checkout P,Continue to add please enter B:')  #After buying a query, continue to add or directly check out
                if choice == 'B':continue
                if cost == 0:break
                for i,j in goods_dict.items():
                    print(i,j)
                buy_choice =input('Confirm purchase, please enter Y,Otherwise quit:')
                if buy_choice != 'Y':break
                print('Total cost:',cost,'The balance is:',balance)

                with open(r'{}.txt'.format(name),mode='w',encoding='utf-8') as f:  #Write balance to file
                    res='|'.join([name,pwd,str(balance)])
                    f.write(res)
                    f.flush()

                break
        else:
            print('Not logged in!')

    else:
        print('Input error, please input again')
Noodle cart that thinks it's ok

Posted by endlyss on Fri, 25 Oct 2019 09:11:35 -0700