search
HomeBackend DevelopmentPython TutorialAn example tutorial for writing a shopping mall

An example tutorial for writing a shopping mall

Jun 20, 2017 pm 04:19 PM
writeshopping 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)][,)  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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python vs. C  : Memory Management and ControlPython vs. C : Memory Management and ControlApr 19, 2025 am 12:17 AM

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python for Scientific Computing: A Detailed LookPython for Scientific Computing: A Detailed LookApr 19, 2025 am 12:15 AM

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Python and C  : Finding the Right ToolPython and C : Finding the Right ToolApr 19, 2025 am 12:04 AM

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python for Data Science and Machine LearningPython for Data Science and Machine LearningApr 19, 2025 am 12:02 AM

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Learning Python: Is 2 Hours of Daily Study Sufficient?Learning Python: Is 2 Hours of Daily Study Sufficient?Apr 18, 2025 am 12:22 AM

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Python for Web Development: Key ApplicationsPython for Web Development: Key ApplicationsApr 18, 2025 am 12:20 AM

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python vs. C  : Exploring Performance and EfficiencyPython vs. C : Exploring Performance and EfficiencyApr 18, 2025 am 12:20 AM

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools