python控制语句实验报告,python流程控制语句案例练习

最近玩python,为了更好的学习其中的流程控制语句,本身写了一个查询员工信息和更新信息的脚本,此脚本仅供练习使用。大拿可绕行!python

#!/usr/bin/env python

import sys

import os

os.system('clear')

mess = '''

\033[;35m ID :%s\033[0m

\033[;35m Name :%s\033[0m

\033[;35m Dep :%s\033[0m

'''

info = '''

\033[;32m+++++++++++++++++++++++++++++++++++++++++++\033[0m

\033[;32m++ Welcome to staffs infomation system ++\033[0m

\033[;32m+-----------------------------------------+\033[0m

\033[;32m+ +\033[0m

\033[;32m+ S: search staffs infomation +\033[0m

\033[;32m+ U: update staffs infomation +\033[0m

\033[;32m+ Q: quit +\033[0m

\033[;32m+ +\033[0m

\033[;32m+++++++++++++++++++++++++++++++++++++++++++\033[0m

'''

info2 = '''

\033[;32m+++++++++++++++++++++++++++++++++++++++++++\033[0m

\033[;32m+ A: Add staffs infomation +\033[0m

\033[;32m+ D: Del staffs infomation +\033[0m

\033[;32m+ Q: quit +\033[0m

\033[;32m+++++++++++++++++++++++++++++++++++++++++++\033[0m

'''

while True:

username = raw_input('\033[;32mPlease input your name: \033[0m').strip()

passwd = raw_input('\033[;32mPlease input password: \033[0m').strip()

if username != 'zhaohh' or passwd != '111111':

print ('\033[;31mYour username not exist or your password wrong!\033[0m')

continue

else:

print '\033[;32mHello zhaohh,Welcome!\033[0m'

break

os.system('sleep 2')

os.system('clear')

print info

while True:

user_input = raw_input("\033[;32mPlease select (S,U,default Q): \033[0m").strip()

# search staffs infomation

if user_input == "s" or user_input == "S":

staff_list = open("file.txt","rw")

c = staff_list.readlines()

staff_list.close()

while True:

inputs = raw_input('\033[;32mPlease input ID or name or dep: \033[0m').strip()

for line in c:

field = line.split()

if inputs in field:

print mess %(field[0],field[1],field[2])

break

else:

juge = raw_input('\033[;31mCan not found information! Continue? yes/no. \033[0m').strip()

if juge == 'yes' or juge == 'y':

continue

else:break

juge = raw_input('\033[;32mContinue search ? yes/no. \033[0m')

if juge == 'yes' or juge == 'y':

continue

elif juge == 'no' or juge == 'n' or juge == '':

sys.exit()

break

break

# add staffs infomation

elif user_input == "u" or user_input == "U":

print info2

while True:

update_input = raw_input('\033[;32mPlease select (A,D,default Q): \033[0m').strip()

if update_input == "a" or update_input == "A":

info_input = raw_input("\033[;32mPlease input user infomation: \033[0m")

staff_list = open("file.txt","a")

staff_list.write(info_input + '\n')

staff_list.close()

juge = raw_input('\033[;32mUpdate successful! Continue search ? yes/no. \033[0m')

if juge == 'yes' or juge == 'y':

continue

elif juge == 'no' or juge == 'n' or juge == '':

sys.exit()

# delete staffs infomation

elif update_input == "d" or update_input == "D":

inputs = raw_input('\033[;32mPlease input ID or name or dep: \033[0m')

f = open("newfile.txt","a")

staff_list = open("file.txt","rw")

for line in staff_list.readlines():

for field in line.split():

if inputs == field:

print '\033[;32mDelete successful!\033[0m'

break

else:

f.write(line)

staff_list.close()

f.close()

os.remove('file.txt')

os.rename('newfile.txt','file.txt')

juge = raw_input('\033[;32mContinue? yes/no. \033[0m')

if juge == 'yes' or juge == 'y':

continue

elif juge == 'no' or juge == 'n' or juge == '':

sys.exit()

elif update_input == "q" or update_input == "Q" or update_input == "":

sys.exit()

else:

print "\033[;31m Input error,try agin!\033[0m"

continue

elif user_input == "q" or user_input == "Q" or user_input == "":

sys.exit()

else:

print "\033[;31m Input error,try agin!\033[0m"

continue

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值