02-python Study Day 2

Keywords: Python Programming Mac

Today, I learned the following aspects, although some of them are not comprehensible, I followed the teacher to write the code.

  1. List, tuple operation
  2. String operation
  3. Dictionary operation
  4. Set operation
  5. File operation
  6. Character Coding and Transcoding

Programming exercises

Please close your eyes and write the following program.

Procedure: shopping cart program


  1. After launching the program, let the user enter the salary, and then print the list of goods.
  2. Allow users to buy goods according to their product number
  3. After the user chooses the commodity, whether the balance is enough or not will be deducted directly and reminded if the balance is insufficient.
  4. You may withdraw at any time. At the time of withdrawal, you can print the purchased goods and the balance.

My Way of Realization

1. Make a product_list to save the goods.
2. Create an empty list as shouping_list
3. Create a new salary list and let the user input the salary. In order to avoid that the user input is not a number, two things are wrong when using int to change the time.
3.1 Determine whether the user entered a number or not
3.2 The user input value is strongly converted to a number, because the subscript of the list is orderly used for judgment.

Create a dead cycle to do something:
1. Show the list of goods to the user after traversing, and ask for the ordinal number. We can use the subscript as the ordinal number.
2. Let the user enter a number and choose the goods to buy.
2.1 To determine whether the user input is a number, if the number, it will be strongly converted into a digital type, stored in the variable user_choice.
2.2 Can be left behind to do: further determine whether the number of user input exceeds the number of goods, if it exceeds the prompt, not more than continue
3. Judging whether the user can afford the product
3.1 Remove items from the list based on the number entered by the user
3.2 Judging affordability
3.3 Add the list to the shopping list shouping_list if you can afford it
3.4 deduction
If you can't afford it:
Tips XX goods can not afford, your money is not enough xxx
2.3 You can leave it behind and do it later: If the value entered by the user exceeds the quantity of the goods, it will prompt you that there is no such goods.
4. Judging whether the user input q
4.1 If the user enters Q
4.2 Show Shopping List
4.3 Display Balance
4.4 Withdrawal Procedure
4.5 Display errors if other characters are entered by the user

Code part

# -*- coding:utf-8 -*-
# Author:Dwdar

product_list = [  # Save the list of goods
    ('Iphoe', 5800),
    ('Mac Pro', 9800),
    ('Bike', 9800),
    ('Watch', 10600),
    ('Coffe', 31),
    ('Alex Python', 120),

shouping_list = []  # An empty list, he's a shopping cart, where the user's purchases are placed.

salary = input("Please enter salary:")  # User Input Salary
if salary.isdigit():  # Determine whether the user entered a number or not
    salary = int(salary)  # If it's a number, it's int Strong to Number

    while True:  # Enter the Dead Cycle
        for index, item in enumerate(product_list):  # Traveling through the list of goods,
            # print(product_list.index(item),item) #Low efficiency
            print(index, item)
        user_choice = input("Choose commodity>>>:")  # Let users choose goods
        if user_choice.isdigit():  # If the user enters a number
            user_choice = int(user_choice)  # If it's a number, it's int Strong to Number
            # Determine whether the number entered by the user exceeds the number and quantity of the merchandise.
            if user_choice < len(product_list) and user_choice >= 0:
                p_item = product_list[user_choice]  # Take out the price of a commodity by subscribing
                if p_item[1] <= salary:  # Judging affordability
                    shouping_list.append(p_item)  # If you can afford it, store it in a shopping cart.
                    salary -= p_item[1]  # Deduct money
                    # Show the user what to buy and what the balance is.
                    print("commodity %s Added to shopping cart,Your balance is: \033[31;1m%s\033[0m" % (p_item, salary))
                else:  # If you find that you can't afford it at the time of judgment
                    print("\033[41;1m Your balance is only left.[%s]Come on, buy a wool.\033[0m" % salary)  # Feedback to Users Can't Buy
                # Another situation is that the user does not have the product after selection and prompts the user.
                print("\033[41;1m Goods of your choice%s Non-existent!\033[0m" % user_choice)

        elif user_choice == 'q':  # If the user enters q,Enter exit link
            print("----------------List of commodities----------------")  # List title, for a better look
            for p in shouping_list:  # Traverse the list of purchased items
                print(p)  # Print the list of goods
            print("\033[31;1m Your balance is:%s\033[0m" % salary)  # Prompt the user about the balance
            exit()  # Exit procedure
            print("Selection error")  # If the user does not enter q,And when we enter something else, we prompt the user

Posted by buildernaut1 on Thu, 10 Oct 2019 06:48:32 -0700