An example tutorial for writing a shopping mall
Assignment: Shopping mall
Product display, price
Buy, add to shopping cart
Payment, money is not enough
The flow chart is as follows:
The code has 4 files in total, as follows:
User file:
alex 666geng 888zhang 222lou 250zeng 333
Product file:
10001 小米3 2699 5010002 比亚迪宋 100001 9110003 格力变频空调 20000 410004 TCL电视 6000 9810005 联想001 5600 99210006 跑步鸡 250 66310007 大众 58000 9710008 马自达 68000 43
Shopping cart file:
10002 比亚迪宋 100001 1 geng10006 跑步鸡 250 1 geng10008 马自达 68000 1 geng10003 格力变频空调 20000 1 geng
Wallet file:
geng 115880zeng 126748
The code is as follows:
==, end=,) line =+,shopping_name+,shopping_price+,shopping_stock+=,) user_line == user = input( pwd == %== stock = input( (num) <= (shoppings[stock_list.index(stock)][],shoppings[stock_list.index(stock)][] = str((shoppings[stock_list.index(stock)][]) - (num) > (shoppings[stock_list.index(stock)][,) shop_list = + =,) line = username ==(price)*= cost +== input(=,) line = username === str((bal) + ,) user_wallet .join(user_wallet) + ===,) line = username == (purse) >= = str((purse) - __name__ == == *******************************************************************************************[;1m欢迎来到小猪猪购物网站,祝你购物愉快!\*******************************************************************************************== input(== lists[= lists[= input( shopping_index == =,) shopping_list = .join(shopping_list) + ==,) user_line .join(user_line) + ) 运行结果如下: ******************************************************************************************* 欢迎来到小猪猪购物网站,祝你购物愉快! ******************************************************************************************* 商品编号: 商品名称: 商品价格: 商品库存: ************************************************************************************* 10001 小米3 2699 50 10002 比亚迪宋 100001 92 10003 格力变频空调 20000 5 10004 TCL电视 6000 98 10005 联想001 5600 992 10006 跑步鸡 250 664 10007 大众 58000 97 10008 马自达 68000 44 请输入你的用户名:geng 请输入你的密码:888 您好,欢迎geng,购物愉快! 请输入你要购买商品的编号(输入quit退出购物):10002 请输入你要购买商品的数量:1 请输入你要购买商品的编号(输入quit退出购物):10006 请输入你要购买商品的数量:1 请输入你要购买商品的编号(输入quit退出购物):10008 请输入你要购买商品的数量:1 请输入你要购买商品的编号(输入quit退出购物):10003 请输入你要购买商品的数量:1 请输入你要购买商品的编号(输入quit退出购物):quit 对不起,你的余额不足,请充值 请输入你要充值的金额:100000 支付成功!
The running process of the above code is as follows:
(1) Display product information;
(2) User login verification;
(3) The user enters the product and quantity they want to purchase, and enters quit to exit shopping;
(4) Add to shopping cart file;
(5) Settlement, go to the shopping cart to calculate the shopping cost;
(6) Call the user wallet file to see if the balance is enough to pay;
(7) The balance is greater than or equal to the same number of purchases, and the payment is successful; the balance is not enough, the user recharges;
(8) Call the recharge module to recharge;
(9) Recursion, it is judged that the balance after the user's recharge is greater than or equal to the cost of this shopping, the payment is successful;
(10) End the program.
Master the knowledge:
(1) List traversal method, list modification is to first read into a list, and then according to the user Modify the input information;
(2) Open and close files back and forth;
(3) Apply recursion, that is, when When the user's balance is always small, recurse until the user's balance is greater than the shopping cost;
(4) The beginning and end of the loop (break), the beginning and end of the program Termination (sys.exit), the start and termination of the function (return);
(5) The association and indexing of lists, and the problem of the order in which files are read.
The above is the detailed content of An example tutorial for writing a shopping mall. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use C# to write a Bloom filter algorithm. The Bloom Filter (BloomFilter) is a very space-efficient data structure that can be used to determine whether an element belongs to a set. Its basic idea is to map elements into a bit array through multiple independent hash functions and mark the bits of the corresponding bit array as 1. When judging whether an element belongs to the set, you only need to judge whether the bits of the corresponding bit array are all 1. If any bit is 0, it can be judged that the element is not in the set. Bloom filters feature fast queries and

How to write exponentiation function in C language Exponentiation (exponentiation) is a commonly used operation in mathematics, which means multiplying a number by itself several times. In C language, we can implement this function by writing a power function. The following will introduce in detail how to write a power function in C language and give specific code examples. Determine the input and output of the function The input of the power function usually contains two parameters: base and exponent, and the output is the calculated result. therefore, we

The hotel reservation system is an important information management system that can help hotels achieve more efficient management and better services. If you want to learn how to use C++ to write a simple hotel reservation system, then this article will provide you with a basic framework and detailed implementation steps. Functional Requirements of a Hotel Reservation System Before developing a hotel reservation system, we need to determine the functional requirements for its implementation. A basic hotel reservation system needs to implement at least the following functions: (1) Room information management: including room type, room number, room

How to write a simple minesweeper game in C++? Minesweeper is a classic puzzle game that requires players to reveal all the blocks according to the known layout of the minefield without stepping on the mines. In this article, we will introduce how to write a simple minesweeper game using C++. First, we need to define a two-dimensional array to represent the map of the Minesweeper game. Each element in the array can be a structure used to store the status of the block, such as whether it is revealed, whether there are mines, etc. In addition, we also need to define

How to use C# to write dynamic programming algorithm Summary: Dynamic programming is a common algorithm for solving optimization problems and is suitable for a variety of scenarios. This article will introduce how to use C# to write dynamic programming algorithms and provide specific code examples. 1. What is a dynamic programming algorithm? Dynamic Programming (DP) is an algorithmic idea used to solve problems with overlapping subproblems and optimal substructure properties. Dynamic programming decomposes the problem into several sub-problems to solve, and records the solution to each sub-problem.

How to use C++ to write a simple student course selection system? With the continuous development of technology, computer programming has become an essential skill. In the process of learning programming, a simple student course selection system can help us better understand and apply programming languages. In this article, we will introduce how to use C++ to write a simple student course selection system. First, we need to clarify the functions and requirements of this course selection system. A basic student course selection system usually includes the following parts: student information management, course information management, selection

How to write KNN algorithm in Python? KNN (K-NearestNeighbors, K nearest neighbor algorithm) is a simple and commonly used classification algorithm. The idea is to classify test samples into the nearest K neighbors by measuring the distance between different samples. This article will introduce how to write and implement the KNN algorithm using Python and provide specific code examples. First, we need to prepare some data. Suppose we have a two-dimensional data set, and each sample has two features. We divide the data set into

How to use C# to write a binary search algorithm. The binary search algorithm is an efficient search algorithm. It finds the position of a specific element in an ordered array with a time complexity of O(logN). In C#, we can write the binary search algorithm through the following steps. Step 1: Prepare data First, we need to prepare a sorted array as the target data for the search. Suppose we want to find the position of a specific element in an array. int[]data={1,3,5,7,9,11,13
