The actual application of string strip (), and split (), stripsplit

Source: Internet
Author: User

The actual application of string strip (), and split (), stripsplit

Shop_list = []
Shop_cart = []
Product_list = open ("test.txt", 'a + ') # Read the product in the file
Product_list.seek (0, 0)
For line in product_list.readlines ():
(Product, price) = line. strip (). split (",") # split the commodities read cyclically and assign values
Shop_list.append ([product, int (price)]) # Write list

Print (shop_list)
Product_list.close ()

Money = open ("money.txt", 'a + ') # list of purchased items
'''Money. seek (0, 0)
Money1 = int (money. readline ())
If money1 <= 0 :'''
Salary = input ('Enter the amount :')
If salary. isdigit (): # judge whether the input value is a number.
Salary = int (salary)
Else:
Exit ()
'''Else:
Print ("the amount you have not consumed is", money1)
Salary = money1 '''
While True:
For index, I in enumerate (shop_list): # print the shopping list with an index number
Print (index, I)
Choose = input ('select the product number you want to purchase :')
If choose. isdigit (): # determine whether the input value is a number.
Choose = int (choose)
If choose <len (shop_list) and salary> = shop_list [choose] [1]: # determine whether the input index number in the list is greater than the price of the item
Salary-= shop_list [choose] [1] # redefine salary after purchase
Print ('added {thing} to the shopping cart and your remaining balance {salary} '. format (thing = shop_list [choose] [0], salary = salary ))
Shop_cart.append (shop_list [choose]) #### add to shopping cart list
Continue
If choose <len (shop_list) and salary <= shop_list [choose] [1]: ### processing when the money is insufficient
Print ("your current balance is {salary} is insufficient to purchase this item. Please reselect it! ")
Else:
Print ('the selected item does not exist. Please reselect it! ') ### The input index is not processed in the list
Elif choose = 'q': ### Exit Processing
Center = 'shop list'
Print (center. center (30 ,'-'))
Sum = 0 # Set the total consumption value to 0 first
Money. write ('% s \ n' % str (shop_cart) # write the purchased item to a file!
Money. close ()
For spend in shop_cart: # Use the for loop to calculate the total consumption value!
If _ name _ = '_ main __':
Sum + = spend [1]
For I in shop_cart: # print the items in the current shopping cart
Print (I)
Print ('your current balance is ', salary)
Print ("total consumption", sum)
'''Money. write (str (salary ))
Money. close ()'''
Exit ()
Else:
Print ('incorrect input! Enter again! ')

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.