SoFunction
Updated on 2024-11-13

Python implementation of a simple shopping cart applet

In this article, we share the example of Python to achieve a simple shopping cart applet specific code for your reference, the details are as follows

request

coding

# --*--coding:utf-8--*--
# Author: village rain

import pprint

productList = [('Iphone 8', 10000),
               ('GTX2080', 8000),
               ('Z7KP7-GT', 6000),
               ('Mac pro', 15000),
               ('Honor 10', 2800),
               ('Iphone XR', 12000),
               ('Mi 8', 2999)
               ]

shoppingList = []

print('Enter your salary:')
salary = input()
if not ():
    print('Please enter an integer')
else:
    salary = int(salary)
    while True:
        for index, item in enumerate(productList):
            print(index + 1, item)
        print('Enter the serial number of the item you want to buy:')
        userWant = input()
        if ():
            userWant = int(userWant)
            if userWant <= len(productList) and userWant > 0:
                print('You are buying:', productList[userWant - 1][0])
                if salary >= productList[userWant - 1][1]:
                    (productList[userWant - 1][0])
                    salary -= productList[userWant - 1][1]
                    print('You have purchased' + productList[userWant - 1][0] + ', your balance is ' + str(salary))
                else:
                    print('Sorry, your balance is not enough! Please work hard!')
                    print('Your current purchase is:')
                    for brought in shoppingList:
                        (brought)
                    print('Your current balance is:', salary)
                    exit()
            else:
                print('There is a mistake in the serial number of the product you entered, please re-enter it')
        elif userWant == 'q':
            print('-----------Shopping List----------')
            for brought in shoppingList:
                (brought)
            print('Your balance is ', salary)
            exit()
        else:
            print('Invalid input!!!')

in the end

This is the whole content of this article, I hope it will help you to learn more.