xywuyiba6 发表于 2018-8-9 09:57:58

Python [1] 基础理论概述

#!/usr/bin/env python  
#Author:Allentuns
  
#Date:20150119
  
#codind:utf8
  

  
import os
  
import re
  

  
txl = []
  
def menu():
  
      print '''
  
      1.add
  
      2.display
  
      3.search
  
      4.remove
  
      0.exit
  
      '''
  
      op = raw_input('select op: ')
  
      return op
  

  
#def add():
  
#       name = raw_input('Please input name : ')
  
#       age = raw_input('Please input age : ')
  
#       tel = raw_input('Please input tel : ')
  
#       txl.append()
  

  
def add():
  
      while True:
  
                name = raw_input('Please input name :').strip()
  
                if len(name) == 0:
  
                        print 'no invalid user,please continue.'
  
                        continue
  
                else:
  
                        break
  
      while True:
  
                try:
  
                        age = int(raw_input('Please input age :'))
  
                except ValueError:
  
                        print 'invalid age.Please continue.'
  
                        continue
  
                else:
  
                        break
  
      while True:
  
         tel = raw_input('please input tel :')
  
         res = r'^1\d{10}$'
  
         c = len(re.findall(res,tel))
  
         if c == 0:
  
             print 'invalid tel.Please continue'
  
             continue
  
         else:
  
             break
  

  
      age = str(age)
  
      txl.append()
  

  
def display():
  
         print 'name\tage\ttel'
  
         print '+'*25
  
         for i in txl:
  
                print '%s\t%s\t%s' % tuple(i)
  

  
def search():
  
      name = raw_input('Please input name : ')
  
      print 'name\tage\ttel'
  
      print '+'*25
  
      for i in txl:
  
                if i == name:
  
                        print '%s\t%s\t%s' % tuple(i)
  

  
def remove():
  
      name = raw_input('Please input name : ')
  
      for i in txl:
  
                if i == name:
  
                        txl.remove(i)
  
                        write_txl()
  

  
def get_txl():
  
      if os.path.exists('txl.db'):
  
                fp = file('txl.db','r')
  
                s = fp.read()
  
                for i in s.split('\n'):
  
                        txl.append(i.split(','))
  
                fp.close()
  
                ctxl = len(s)
  
                return ctxl
  
      else:
  
                pass
  

  
def write_txl():
  
      tmp = []
  
      fp = file('txl.db','w')
  
      for i in txl:
  
                tmp.append(','.join(i))
  
      str = '\n'.join(tmp)
  
      fp.write(str)
  
      fp.close()
  

  
get_txl()
  
while True:
  
      op = menu()
  
      if op == '1':
  
                add()
  
                write_txl()
  
      elif op == '2':
  
                display()
  
      elif op == '3':
  
                search()
  
      elif op == '4':
  
                remove()
  
      elif op == '0':
  
                break
页: [1]
查看完整版本: Python [1] 基础理论概述