Python note-3 (Purchase Product), python-3

Source: Internet
Author: User

Python note-3 (Purchase Product), python-3

#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-

# The following is a shopping program:
# First enter the salary, display the commodity list, purchase, quit, and finally format and output the purchased commodity.
Count = 0
While True: # make a loop judgment. If the input is not a number, exit after three times based on the prompt.
Salary = input ("input your salary:") # Enter your salary
If salary. isdigit (): # The input salary must be a number to go down.
Salary = int (salary) # convert to integer type
Break
Else:
Print ("Please input a number :")
Count + = 1
If count = 3:
Exit ()

Goods_list = [["Iphone", 5800], ["Macbook", 12800], ["iMac", 15000], ["ApplePen", 500], ["IPod ", 1200] # product list
Shop_list = [] # list of purchased products
Msg = "Product List"
Print (msg. center (30 ,"*"))
For I, ele in enumerate (goods_list): # traverse the elements in the sequence and Their subscript
Print (I, ".", ele [0], ele [1])
While True:
Choice = input ("Which do you want (quit type \" quit \ ") >>> ")
If choice. isdigit (): # determine whether the selected number is a number.
Choice = int (choice) # convert to integer type
If choice <= len (goods_list)-1: # The selected number must be smaller than the list length-1, because the subscript starts from 0.
If salary> = int (goods_list [choice] [1]): # judge whether the salary is sufficient
Shop_list.append (goods_list [choice]) # add the item to shopList
Salary-= goods_list [choice] [1] # deduct salary
Print ("You just buy a % s now you have % s RMB" % (goods_list [choice] [0], salary ))
Else:
Print ("Not enough money ")
Else:
Print ("There is no such things ")
Elif choice = "quit ":
Print ("Here is what you buy:") # The idea Here is to create a dictionary to format and output the purchased item
Total = 0
Shop_dict = {}
For item in shop_list:
Things = item [0]
Money = item [1]
Total + = int (money)
If things in shop_dict:
Shop_dict [things] [0] + = 1
Shop_dict [things] [1] + = money
Else:
Shop_dict [things] = [1, money]
For item in shop_dict.items ():
Print ("% s total % s" % (item [0], item [1] [0], item [1] [1])
Print ("total spent:", total)
Exit ()
Else:
Print ("Please input a number ")
Continue

Related Article

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.