Python Small program--shopping

Source: Internet
Author: User

Flow chart

Code Program

saving = Int (input (' Please enter your salary: '))
shopping = [' iphone ', 5800],[' mx6 ', 2000],[' Pythonbook ', 80],[' surface ', 12000],[' coffe ', []] #商品列表
count = 0 #商品列表编号
shoppinglist = [] #存储购买的商品
For I in shopping: #给商品列表加上标签1 2 ....
Count +=1
print (count,i)

print (' Press-1 to exit the purchase ') #退出购物循环
While True:
number = Int (input (' Please enter the Product ID: '))
if saving <= 0 or number = =-1: #检查余额和退出命令
Break
Elif number not in range (1,len (shopping) +1): #检查输入的合法性
print (' input error, new input ')
Continue
print (number,shopping[number-1][1]) #提示选择的商品编号和价格
if shopping[number-1][1] < saving: #商品的价值与输入的工资进行比较
print (Shopping[number-1:number]) #提示选择的商品价格
shoppinglist.append (Shopping[number-1:number]) #已经选好的商品加入购物车
Saving-= shopping[number-1][1] #余额等于工资减去购买商品的价价格
print (' balance ', saving) #提示余额

Else:
print (' Boy you pay less, buy a little cheaper ') #余额不足, warm tips

For i in shoppinglist: #打印购买成功的商品
print (i)
print (' balance ', saving) #打印余额

Summary points:
1. Flowchart can help me understand the sequence of programming logic more quickly
2. Consider that the product list is dynamic and can be added or deleted
3. Consider the legality of user input
4. Consider whether the user input is repeated input
5. Record the item selected by the user and indicate the remaining balance
6. User exit, print the product information and balance that the user chooses good
    

Python applet--shopping

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.