界面
代码
import prettytable as pt
import os. path
filename = 'ticket.txt'
def update ( row_num) : with open ( filename, 'w' , encoding= 'utf-8' ) as wfile: for i in range ( row_num) : lst1 = [ f' { i+ 1 } ' , '有票' , '有票' , '有票' , '有票' , '有票' ] wfile. write( str ( lst1) + '\n' )
def show_ticket ( ) : with open ( filename, 'r' , encoding= 'utf-8' ) as rfile: lst = rfile. readlines( ) tb = pt. PrettyTable( ) tb. field_names = [ '行号' , '座位1' , '座位2' , '座位3' , '座位4' , '座位5' ] for i in lst: tb. add_row( eval ( i) ) print ( tb)
def order_ticket ( row, column) : with open ( filename, 'r' , encoding= 'utf-8' ) as rfile: lst = rfile. readlines( ) with open ( filename, 'w' , encoding= 'utf-8' ) as wfile: for item in lst: item= eval ( item) if item[ 0 ] == row: item[ int ( column) ] = '已售' print ( item) wfile. write( str ( item) + '\n' ) else : wfile. write( str ( item) + '\n' )
if __name__ == '__main__' : row_num = 13 update( row_num) show_ticket( ) while True : choose_num = input ( '请输入选择的座位,如13,5表示13排五号座位' ) try : row, column = choose_num. split( ',' ) order_ticket( row, column) except : print ( '输入格式有误,如13排五号座位,应输入13,5,请重新输入' ) continue else : choose = input ( '是否继续购票?y/n' ) if choose == 'y' : continue else : show_ticket( )