Refactoring Optimization python shopping cart

Source: Internet
Author: User

# -*- coding:utf-8 -*-def regiter ():    while true:         username = input ("Please enter your account number:"). Strip ()          password = input ("Please enter your password:"). Strip () #去除空格及换号符          with open (' register ', encoding= ' UTF-8 ') as f1:       &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;FOR&NBSP;LINE&NBSP;IN&NBSP;F1: #循环读取注册文件中的内容                  line_list=line.strip (). Replace (', ', ', '). Split (', ') #去除空格及换号符以及把中文逗号换成英文逗号                   #print (Type (line_list))                  if username==line_list[0]:             &nbsP;       print ("User name already exists, please re-enter")                      break             else:                 with open (' register ',  encoding= ' UTF-8 ', mode= ' a ') as f2 :                     f2.write (' \n{},{} '. Format (Username,password))                      print ("Congratulations on your registration success%s"%username)                       return True  #结束函数def  login ():     i=0# counter     while  i<3: #超过3次后, Login failed  &Nbsp;      username = input ("Please enter your account number:"). Strip ()          password = input ("Please enter your password:"). Strip ()   #  remove space and change symbol          with open (' register ', encoding= ' UTF-8 ') as f1:   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;FOR&NBSP;LINE&NBSP;IN&NBSP;F1: #循环读取注册文件中的内容                  line_list=line.strip (). Replace (', ', ', '). Split (', ') #去除空格及换号符以及把中文逗号换成英文逗号                   #print (Type (line_list))                  if username==line_list[0]and password==line_ list[1]:                     print ("* * *Login success ******* ")                      return True             else:                 print ("Incorrect account or password entry")                  i+=1def shopping ():     list_he=[]    offer=input (" Please enter your stored value card amount: "). Strip ()     if offer.isdigit ():         offer=int (offer)         while True:             shipin2 = [[' milk ',  20], [' jerky ',  30], [' rice ',  15], [' bread ',  15], [' beer ', 3.5]]            &nbsP;for i, a in enumerate (shipin2, 1):  #  cycle Print product List                  print ("Ordinal:%s"  % i,   "Goods:%s"  % a[0],  "Price:%s Yuan"  % a[1])              huo_qu = input ("Please enter the item you want to buy, enter [0] to exit"). Strip ()              if huo_qu.isdigit ():                 huo_qu=int (Huo_qu)                  if huo_qu > 0 and  huo_qu <= len (shipin2):  #  Verify input is correct                      j = shipin2[huo_ qu - 1]  #  PurchaseBuy Goods and Prices                      if j[1] > offer:  #  determine if the price of the item you want to buy exceeds the balance                           print ("Your balance is insufficient, please recharge in time")                      else:                         offer =  offer - j[1]  #  calculate the price after the purchase of the goods                          print ("The item you purchased is %s " % j[0], " Remaining amount is%s " % offer)   #  output purchased items                          list_he.append (J[0])    #  add purchased items to the collection                          print ("You have purchased%s"  % list_he)   #   Purchased goods collection                          continue                 elif huo_qu == 0:                     print ("Exit program, Goodbye")                       for m in list_he:                         print ("You have purchased%s"  % m)                      break                 else:                      Print ("The mall cargo is temporarily short, please enter the correct product serial number")     else:        print ("You have entered an illegal character, please re-enter") choice_dict={    1:regiter,    2:login,     3:shopping}while true:    print ("-------Welcome to Shopping malls-------           1: Registration            2: Login           3: Shopping            4: Exit            ')     choice=input ("Please select the serial number"). Strip ()     if choice.isdigit ():         choice =int (choice)         if choice>0 and choice<=len ( choice_dict):             choice_dict[choice] ()         else:             print ("You entered out of range")     else:         print ("You have entered an illegal character")


Refactoring Optimization python shopping cart

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.