python作业7

24.爬楼梯问题

 

25.汉诺塔

26.不死兔子等等

27.作业(选做题):

         某个人进入如下一个棋盘中,要求从左上角开始走,

         最后从右下角出来(要求只能前进,不能后退),

         问题:共有多少种走法?

         0 0 0 0 0 0 0 0

         0 0 0 0 0 0 0 0

         0 0 0 0 0 0 0 0

         0 0 0 0 0 0 0 0

         0 0 0 0 0 0 0 0

              

 

 

 

做一个用户管理系统:

         实现用户注册

         登录

         密码加密

         发表文章

         查寻自己文章

         修改文章

         修改密码

         退出系统

import sys
import hashlib
import os
from os import path
users = []
salt = '……*……68*!¥'
flag = 1 

def main_page():
  print('*~' * 25)
  print('请输入你的选择:')
  print('\t\t1.用户登录')
  print('\t\t2.用户注册')
  print('\t\t3.退出')
  print('*~' * 25)
  choice = int(input('请输入你的选择:'))
  return choice


def register():
  username = input('输入用户名:')
  if username == None or username.strip() == '':
    print('用户名不能为空')
    return
  password = input('输入密码:')
  if password == None or password.strip() == '':
    print('密码不能为空')
    return 
  for i in users:
    if i.get(username) == username:
      print('用户名重复')
      return 
  user = {}
  user['username'] = username
  user['password'] = password_md5(password)
  users.append(user)


def login():
  number = input('请输入用户名:')
  password = input('请输入密码:')
  is_login(number,password_md5(password))


def password_md5(password):
  md5 = hashlib.md5(password.encode("utf-8"))
  md5.update(salt.encode("utf-8"))
  return md5.hexdigest()


def is_login(username,password):
  global flag
  for i in users:
    if i.get('username') != username and i.get('password') != password:
      print('用户名或密码不正确!')
      return
    elif i.get('username') == username and i.get('password') == password:
      flag = 2
      article_actions()
      break


def article_actions():
  print('*~' * 25)
  print('请输入你的选择:')
  print('\t\t1.发表文章')
  print('\t\t2.查询自己文章')
  print('\t\t3.修改文章')
  print('\t\t4.修改密码')
  print('\t\t5.退出')
  print('*~' * 25)
  choice_1 = int(input('请输入你的选择:'))
  return choice_1


def publish_articles():
  name = str(input('请输入要写入文档名称:'))
  article = str(input('请输入文档内容:\n'))
  load = 'C:\\Users\\Lenovo\\Desktop\\文章\\' + name + '.txt'
  f = open(load, 'wt', encoding = 'utf-8')
  if f.writable():
    f.write(article)
  if not f.closed:
    f.close()


def scanner_files(url):
  files = os.listdir(url)   
  for f in files:
    try:
      real_path = path.join(url, f) 
      if path.isfile(real_path):
        if real_path.endswith('.txt'):
          print(f'可读的文章有:{f}')     
      elif path.isdir(real_path):
        scanner_files(real_path)
    except PermissionError as e:
      pass


def query_articles():
  scanner_files('C:\\Users\\Lenovo\\Desktop\\文章')
  name = str(input('请选择要读文档名称:'))
  load = 'C:\\Users\\Lenovo\\Desktop\\文章\\' + name + '.txt'
  f = open(load, "rt", encoding="UTF-8")
  content = ""
  if f.readable():
    content = f.read()
  print(f'读到到数据:{content}')
  if not f.closed:
    f.close()


def revise_article():
  scanner_files('C:\\Users\\Lenovo\\Desktop\\文章')
  name = str(input('请选择要修改文档名称:'))
  load = 'C:\\Users\\Lenovo\\Desktop\\文章\\' + name + '.txt'
  old_str = str(input('请输入要改的地方:'))
  new_str = str(input('请输入改正后的语句:'))
  file_data = ''
  f = open(load, "r", encoding="utf-8")
  for line in f:
    if old_str in line:
      line = line.replace(old_str,new_str)
    file_data += line
  f = open(load, "w", encoding="utf-8")
  f.write(file_data)


def change_password():
  username = input('请输入用户名:')
  password = input('输入修改后的密码:')
  if password == None or password.strip() == '':
    print('密码不能为空')
    return
  user = {} 
  user['username'] = username
  user['password'] = password_md5(password)
  users.append(user)


if __name__ == '__main__':
  while True:
    while flag == 1:
      choice = main_page()
      if choice == 1:
        print("登录")
        login()
      elif choice == 2:
        print("注册")
        register()
      elif choice == 3:
        sys.exit()
    while flag == 2: 
      choice_1 = article_actions()
      if choice_1 == 1:
        print('写入文章')
        publish_articles()
      elif choice_1 == 2:
        print('读取文章')
        query_articles()
      elif choice_1 == 3:
        print('修改文章')
        revise_article()
      elif choice == 4:
        print('修改密码')
        change_password()
      elif choice_1 == 5:
        main_page()
        flag = 1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值