开发者学堂课程【Python 入门 2020年版:结构搭建与退出功能的实现】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/639/detail/10432
结构搭建与退出功能的实现
内容介绍:
一、结构搭建
二、三种退出功能
一、结构搭建
l def start():
try:with open('files/welcome.txt','r',encoding='utf8' )as
file:
content = file. read()while True:input(content)except FileNotFoundError:print('文件未找到')if_name__=='_main__':start()
运行程序出现界面
**欢迎来到 xx 管理系统**
1.登 录
2.注 册
3.退 出
读取 file 文件夹的东西
def read_ file(file_ name):with open( ' ./files' + file_ name, 'r', encoding='utf8') asfile:content = file. read()return contentexcept FileNotFoundError:print('文件未找到')def start():content = read_ file( 'welcome .txt' )
while True:input( content)
if_name__=='_main__':start()
读写文件共同读取相对麻烦,对代码进行改进
import file_ managerdef start():content = file_ manager. read_ file( 'welcome.txtwhile True :input(content)]if_name__ =='_ main__'start()
若文件换取名字则读取不到,因此定义一个变量base_ dir = './files/'def read_ file(file_ name):try:with open(base_ dir + file_ name, 'r' , encoding= 'utf8') as file:content = file. read()return contentexcept FileNotFoundError :print('文件未找到')
编写欢迎页面代码base_ dir = './files/'def read_ file(file_ name):try:with open(base_ dir + file_ name, 'r' , encoding= 'utf8') as file:content=file_manager.read__file('welcome.txt')
while True:input(content+'\n请选择(1-3):')if operator =='1':
print('登录')
elif operator ='2':print('注册')elif operator=='3':
print('退出‘)
二、三种退出功能
# break #把死循环停止
# exit(0)#退出整个程序
sys .exit(0)