python如何实现不用装饰器实现登陆器小程序

yipeiwu_com6年前Python基础

这篇文章主要介绍了python如何实现不用装饰器实现登陆器小程序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

下面代码演示了不使用装饰器实现用户登陆功能的小程序,在python3.x下可正常运行

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 26 15:26:58 2019

@author: root
"""

def menu():
  print('choice'.center(50,'-'))
  msg = '''
  1. home
  2. finance
  3. book
  4. exit
  '''
  print(msg)
  print("".center(50,'-'))
 
def login(login_flat,choice):
  if not login_flat:
    while True:
      username = str(input("username:"))
      password = str(input("password:"))
      if choice == '1':
        home()
        with open('jingdong.txt','r') as f1:
          for line in f1:
            element = line.split(" ",1)
            print(element[0])
            print(element[1])
            if username.strip() == element[0].strip() and password.strip() == element[1].strip():
              print("log in successful!")
              return True
            print("failed to log in!,try it again!")
      elif choice == '2':
        finance()
        with open('weixin.txt','r') as f2:
          for line in f1:
            element = line.split(" ",1)
            if username.strip() == element[0].strip() and password.strip() == element[1].strip():
              print("log in successful!")
              return True
            print("failed to log in!,try it again!")   
      elif choice == '3':
        book()
        with open('jingdong.txt','r') as f3:
          for line in f3:
            element = line.split(" ",1)
            if username.strip() == element[0].strip() and password.strip() == element[1].strip():
              print("log in successful!")
              return True
            print("failed to log in!,try it again!")              
  else:
    print("You have logged in before!")
    return True
#@login(login_flat,choice)            
def home():
  print("home.....")
#@login(login_flat,choice) 
def finance():
  print("finance.....")
#@login(login_flat,choice) 
def book():
  print("book.....")

  
login_flag = False
while True:
  menu()
  choice = input("your choice:")
  if choice == '1':
    login_flag = login(login_flag,choice)
  elif choice == '2':
    login_flag = login(login_flag,choice)
  elif choice == '3':
    login_flag = login(login_flag,choice)
  elif choice == '4':
    print('bye-bye')
    break
  else:
    print("wrong input ,try it again!")

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Django ORM框架的定时任务如何使用详解

Django ORM框架的定时任务如何使用详解

前言 大家在Django项目开发过程中,是不是也经常遇到这样的场景:需要实现一个定时任务,但又不想脱离Django环境独立运行,如:还需要使用Django的ORM框架操作Models类、...

python异常触发及自定义异常类解析

python程序运行中,可由程序抛出异常。 异常触发:使用raise命令抛出异常,即可使用异常基类Exception,也可使用自定义异常类(继承Exception类)。 class...

python每天定时运行某程序代码

思路:利用time函数返回的时间字符串与指定时间字符串做比较,相等的时候执行对应的操作。不知道大家的思路是什么,感觉这样比较耗CPU。。。。 此处设置为15:30:10 输出相应内容,需...

Python3 关于pycharm自动导入包快捷设置的方法

Python3 关于pycharm自动导入包快捷设置的方法

正常开发的时候,我们都手动去写要引入到包,有过java开发的同事,用过快捷键ctrl + alt + o 会自动引入所有的依赖包,pycharm也有这样的设置,看看怎么设置吧。 设置快...

python中如何使用正则表达式的非贪婪模式示例

前言 本文主要给大家介绍了关于python使用正则表达式的非贪婪模式的相关内容,分享出来供大家参考学习,下面话不多说了,来一起详细的介绍吧。 在正则表达式里,什么是正则表达式的贪婪与非贪...