Started on the road a little

Source: Internet
Author: User
Tags haproxy

Xiao Hunter, formerly known as Rabbit, Master of Bioengineering. Because of the boredom and low wages, so in the old boy on the side of a class, 6840, began my python journey, toward the field of bioinformatics "March."

From the very beginning to be very repulsive programming, very annoying debug, to later will be self-check data, check code, Debug. After submitting 3 assignments, I slowly discovered that I could actually do it well.

Those programming Daniel, feel like a martial arts fiction in the heroes, minutes to solve the problem. No move is already, once the move, you can be a deadly, especially cool. I also want to quickly become such a person, but, after all, the foundation is poor, and no foundation, can only start from the basic skills. Take "martial arts can not work, the end of an empty" to encourage themselves to lay a solid foundation.

The first week is the shopping cart program, the knowledge points used are dictionary, file read and write, level three menu. The dictionary is a powerful tool in Python, which looks like {"key": "Value"}, and what is strong is that the value can be accessed through the key value and can be structured to store information. Level Three menu is more difficult, with a lot of while and for loop (at that time will not write functions, the idea is not clear), silly to write a lot of loops.

data={' ou Shang ': {' fruit ': {' cherry ': {' import ': [' $100/kg '], ' domestic ':                [' $50/kg ']}, ' Mango ': {' import ': [' $50/kg '], ' homemade ': [' 20/kg ']                    }, ' Apple ': {' import ': [' 40/kg '], ' homemade ': [' 20/kg '], ' Red Fuji ': [' 30/kg '},}, ' vegetables ': {' tomato ': {' Cherry small ': [ ' $20/kg ', ' Great Secret ': [' $30/kg ']}, ' corn ': {' great ': [' 40/kg '                     ], ' Mexico ': [' 30/kg ']}, ' leek ': {' local ': [' $15/kg '],                    ' Shandong ': [' $18/kg '}}, ' chocolate ': {' Ferrero ': {                     ' Classic flavors ': [' $50/kg '], ' cherry flavors ': [' $80/kg ']}, ' Meiji ': {      ' Pure black ': [' $40/kg '],               ' Milk ': [' $30/kg ']}, ' electrical ': {' refrigerator ': { ' Doctor ': [' HK $ '], ' Haier ': [' HK $ ']}, ' air conditioning ': {' beautiful ': [' $ 00 '], ' gree ': [' $ $ ']},}, ' Carrefour ': {' fruit ': {}, ' vegetables '    : {}, ' chocolate ': {}, ' electrical ': {},}, ' Wal-Mart ': {' fruit ': {}, ' vegetable ': {}, ' chocolate ': {}, ' appliance ': {}, }, ' Good and many ': {' fruit ': {}, ' vegetable ': {}, ' chocolate ': {}, ' appliance ': {},},}

      above is my foodie version of the level three menu. The entry and exit codes are as follows:

While not exit_flag:for I in data:print (i) Choice = input (' Please enter the supermarket >>: ') If choice in data: While not exit_flag:for I2 in Data[choice]: print (' \ t ', i2) Choice2 = input (' Please enter required  Type of purchase name >>: ') if Choice2 in Data[choice]: And not exit_flag:for i3 in DATA[CHOICE][CHOICE2]: Print (' \t\t ', i3) Choice3 = input (' Please enter the item you need to purchase >>: '                             If Choice3 in Data[choice][choice2]: for I4 in Data[choice][choice2][choice3]:                        Print (' \t\t\t ', i4) Choice4 = input (' Please enter the symbol you need to buy >>: ') If Choice4 in Data[choice][choice2][choice3]: print (' \t\t\t\t ', Data[choice][choice2] [Choice3]                        [Choice4])                            Choice5 = input (' The last layer, press B to return; press Q to exit ') if Choice5 = = ' B ':Pass #什么都不做, as a placeholder elif choice5 = = ' Q ': Exit_flag = True if choice3 = = ' B ': break elif choice3 = = ' Q ': Exit_f lag = True if Choice2 = = ' B ': break elif Choice2 = = ' Q ': Exit_flag = T Rue

Haha, handing over the first homework, like the baby just learned to crawl, creeping forward.

The second week made a more complex shopping cart, allowing customers to log in, recharge, and buy products. This time learning to be clever, will use the function. Function of this kind of thing, I think it is like long-distance travel, visitors are variables, functions inside the operation is a good road, the final return is the goal. Of course this may not be very image, because the function can have no parameters, the return result is none by default, but it does not matter, it may be understood that the machine will be better. But I always feel that understanding as a travel, will let me write a function of special pleasure, can stipulate it to go here, visit this spot, buy this thing, take a picture here and so on, write up not so boring, haha. Such a complex thing, of course, a main function is required, although I do not know what I put the right, but at least feasible. Show me the main function:

def main ():    balance = login () state =1 while state! =    str (4): state        = input (' Need to recharge ' press 1; To view history, press 2 Direct shopping Please press 3, exit please press 4 '). Strip ()        if state = = ' 1 ':             balance = Recharge ()        elif state = = ' 2 ': With            open (' Shopping History ', ' R ', encoding= ' Utf-8 ') as File:                print (File.read ())        elif state = = ' 3 ':            print (' Start shopping ')            print (balance)            Balance_after_shopping = Shopping (balance)        elif state = = ' 4 ': With            open (' User_data ', ' R ', encoding= ' utf-8 ') As json_file:                user_data = Json.load (json_file)            user_data[current_user][' balance '] = Balance_after_ Shopping            print ('%s ' account, your current account balance is%d, welcome to your next visit '% (Current_user,int (balance_after_shopping))) with            open (' User _data ', ' W ', encoding= ' Utf-8 ') as Json_file:                json_file.write (Json.dumps (User_data))

The login function is called at the beginning of the

    main function, of course, because you need to log in. Do not log in how to buy things, a treasure to buy things are also required to log in first drop. After the login is to ask the customer what action, recharge the words, call the recharge function; View history, directly read the storage history of the file, start shopping, start the shopping process, if you want to quit, then save the history, then quit the program. The shopping procedure is more complicated, because it involves a three-level menu and a customer-selected save. It is still crawling, so the code is written as follows:

def shopping (balance): Exit_flag = False with open (' Product_list ', ' R ', encoding= ' Utf-8 ') as Json_file:data =        Json.load (Json_file) While not exit_flag:choice = Select (data) if choice = = ' B ': Break elif Choice = = ' Q ': Exit_flag = = True else:while not exit_flag:choice2 = SE                    Lect (Data[choice]) if Choice2 = = ' B ': break elif Choice2 = = ' Q ': Exit_flag = True Else:while Not exit_flag:choice3 =                        Select (Data[choice][choice2]) if choice3 = = ' B ': Break if choice3 = = ' Q ': Exit_flag = = True elif choice3 in data[choice][ CHOICE2]: For I4 in (Data[choice][choice2][choice3]): Print (I4, Data[choice][choice2][chOICE3][I4]) While true:choice4 = input (' Please enter the quantity you need to purchase >>: ').                                    Strip () if int (choice4) <= int (data[choice][choice2][choice3][' quantity ')): sub_total = Int (choice4) *int (data[choice][choice2][choice3][' Unit price ')) PRI                                        NT (' Total Price:%d '%sub_total) if sub_total<= int (balance):                                        temp = [Choice3,choice4] Product_list.append (temp)                                        Balance-= sub_total print (' Your current balance is:%d '%balance) Break Else:print (' Current balance is low, please                            Re-enter the amount you need to buy ') else:print (' Not enough inventory ') Choice5 = input (' Last layer, press B to return; press Q to exit ') if Choice5 = = ' B ': Pass elif Choice5 = = ' Q ': Exit_flag = True if Exit_flag = = True:print (product_li St, balance) save_history (product_list) return balance

The third week of Haproxy file processing, can also, mainly related to reading the file after jumping to the next line of the concept, as well as the form of JSON read. Of course The program also contains the query, add, modify, delete these 4 features. Just say skip, the start is also for the loop implementation, continue can help from jumping out of the current loop, into the next round, so it will reach the next line of backend, that is, the server to read the message.

With open (' Haproxy ', ' W ', encoding= ' Utf-8 ') as F1,open (' Haproxy_backup ', ' R ', encoding= ' Utf-8 ') as F2:      flag = False For      F2:          if Line.strip (). StartsWith ("backend") and line.strip () = = "Backend" + backend:              flag = true
   f1.write (")              continue          elif not flag:              f1.write (line)          if flag and Line.strip ():              ret = Line.strip ()              f1.write (")

As a novice can only climb on the ground, or read more books, look at the code written by the great God, practice basic skills every day. Strive for a better future.

The same is for the rookie you are also learning the old boy's Python course, we can communicate a lot. At the same time, I also welcome the great gods to give me the wrong point, if the better way to achieve, share, little woman here grateful.

to practise.

Started on the road a little

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.