Python初学者如何优化首个代码实例?
- 内容介绍
- 文章标签
- 相关推荐
本文共计618个文字,预计阅读时间需要3分钟。
伪创新可改为假创新、虚假创新、冒牌创新。以下是对以下开头内容的简化
原开头:近年来,市场上频繁出现一些打着创新旗号的商品或服务,实则并无创新之处,这种假创新行为引发了消费者和监管部门的广泛关注。
简化近年来,一些标榜创新的商品或服务实为虚假,引发关注。
def login():
import os
while True:
login_username = input('登录==> 用户[退出登录程序q]: ').strip()
if login_username == 'q':
print('''
已退出登录程序!!!!
''')
break
if not os.path.exists('db.txt'):
with open('db.txt', 'w', encoding='utf-8') as _:
...
with open('db.txt','r',encoding='utf-8',) as f:
for line in f:
username,password = line.strip().split(':')
if login_username == username:
while True:
login_password = input('登录==>密码: ').strip()
if login_password == password:
print(f'{login_username} 登录成功!\n')
break
else:
print('密码错误,请重新输入!!!\n')
break
else:
print(f'{login_username} 用户不存在,请重新输入!!!')
# 注册功能
def register():
import os
while True:
register_username = input('注册==> 用户[退出注册程序q]: ').strip()
if register_username == 'q':
print('''
已退出注册程序!!!!
''')
break
register_password = input('注册==> 密码: ').strip()
if len(register_username) == 0 or len(register_password) == 0:
print('非法输入,不能为空!')
continue
if not os.path.exists('db.txt'):
with open('db.txt','w',encoding='utf-8') as _:
...
with open('db.txt','r+',encoding='utf-8') as f:
for line in f:
a1,_ = line.split(':')
if a1 in register_username:
print('该用户已存在,请重新注册!')
break
else:
f.seek(0,2)
f.write(f'{register_username}:{register_password}\n')
print(f'[{register_username}] 注册成功!\n')
# 功能字典
dict_list = {
'0':('退出',None),
'1':('登录',login),
'2':('注册',register),
}
# 功能调用
while True:
for line in dict_list:
print('==>',line,dict_list[line][0])
number = input('请输入功能编号: ')
if number == '0':
print('退出程序!')
break
print('请输入编号,大憨憨!\n') if not number.isdigit() else ...
print('对不起, 该编号不存在\n') if not dict_list.get(number) else dict_list[number][1]()
ps:这是函数利用,当然也有三层架构的方式,不过这些功能完全用不到
#加油干,撸起袖子进步进步!!!
本文共计618个文字,预计阅读时间需要3分钟。
伪创新可改为假创新、虚假创新、冒牌创新。以下是对以下开头内容的简化
原开头:近年来,市场上频繁出现一些打着创新旗号的商品或服务,实则并无创新之处,这种假创新行为引发了消费者和监管部门的广泛关注。
简化近年来,一些标榜创新的商品或服务实为虚假,引发关注。
def login():
import os
while True:
login_username = input('登录==> 用户[退出登录程序q]: ').strip()
if login_username == 'q':
print('''
已退出登录程序!!!!
''')
break
if not os.path.exists('db.txt'):
with open('db.txt', 'w', encoding='utf-8') as _:
...
with open('db.txt','r',encoding='utf-8',) as f:
for line in f:
username,password = line.strip().split(':')
if login_username == username:
while True:
login_password = input('登录==>密码: ').strip()
if login_password == password:
print(f'{login_username} 登录成功!\n')
break
else:
print('密码错误,请重新输入!!!\n')
break
else:
print(f'{login_username} 用户不存在,请重新输入!!!')
# 注册功能
def register():
import os
while True:
register_username = input('注册==> 用户[退出注册程序q]: ').strip()
if register_username == 'q':
print('''
已退出注册程序!!!!
''')
break
register_password = input('注册==> 密码: ').strip()
if len(register_username) == 0 or len(register_password) == 0:
print('非法输入,不能为空!')
continue
if not os.path.exists('db.txt'):
with open('db.txt','w',encoding='utf-8') as _:
...
with open('db.txt','r+',encoding='utf-8') as f:
for line in f:
a1,_ = line.split(':')
if a1 in register_username:
print('该用户已存在,请重新注册!')
break
else:
f.seek(0,2)
f.write(f'{register_username}:{register_password}\n')
print(f'[{register_username}] 注册成功!\n')
# 功能字典
dict_list = {
'0':('退出',None),
'1':('登录',login),
'2':('注册',register),
}
# 功能调用
while True:
for line in dict_list:
print('==>',line,dict_list[line][0])
number = input('请输入功能编号: ')
if number == '0':
print('退出程序!')
break
print('请输入编号,大憨憨!\n') if not number.isdigit() else ...
print('对不起, 该编号不存在\n') if not dict_list.get(number) else dict_list[number][1]()
ps:这是函数利用,当然也有三层架构的方式,不过这些功能完全用不到
#加油干,撸起袖子进步进步!!!

