zoukankan      html  css  js  c++  java
  • day04--作业

    第一题


    分别画出下面两个列表在内存中是如何存放的
    l1=[11,22,[333,444]]
    l2=[11,22,[33,{'name':'egon','age':18}]]

    如下图所示

    第二题


    用户输入姓名、年龄、工作、爱好 ,然后打印成以下格式
    ------------ info of Egon -----------
    Name  : Egon
    Age   : 22
    Sex   : male
    Job   : Teacher
    ------------- end -----------------

    如下:

    #-*- coding:utf-8 -*-
    
    def main():
        name = input('please enter your name:').strip()
        age = int(input('please enter your age:').strip())
        sex = input('please enter your sex:').strip()
        job = input('please enter your job:').strip()
        printinfo(name,age,sex,job)
    
    def printinfo(name,age,sex,job):
        print('{0:-^30}'.format('info of Egon'))
        print('Name:{0}
    Age:{1}
    Sex:{2}
    Job:{3}'.format(name,age,sex,job))
        print('{0:-^30}'.format('end'))
    
    if __name__ == '__main__':
        main()

    运行结果:

    第三题


    用户输入账号密码,程序分别单独判断账号与密码是否正确,正确输出True,错误输出False即可

     代码如下:

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    def main():
    tag = False username
    = input('please enter your account:').strip() if username == 'Egon': passwd = input('please enter your password:').strip() if passwd == 'Alex666': tag = True print(tag) if __name__ == '__main__': main()

     测试用例:

    [{'Egon':'Alex666'},{'Tank':'CCCDDD'},{'Egon':'VIPbnc'}]

    运行结果:

    [root@Kingstar test]# /usr/bin/python test_2.py 
    please enter your account:Egon         
    please enter your password:Alex666
    True
    [root@Kingstar test]#
    
    
    [root@Kingstar test]# /usr/bin/python test_2.py 
    please enter your account:Tank
    False
    [root@Kingstar test]# /usr/bin/python test_2.py 
    please enter your account:Egon   
    please enter your password:VIPbnc
    False
    [root@Kingstar test]# 

     第四题


    让计算机提前记下egon的年龄为18岁,写一个才年龄的程序,要求用户输入所猜的年龄
    ,然后程序拿到用户输入的年龄与egon的年龄比较,输出比较结果即可

     代码如下:

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    def main():
        user_info = {'egon':18}
        guess_age = int(input('please enter the age of egon:').strip())
        if guess_age == user_info.get('egon'):
            print('猜对了!')
        elif guess_age < user_info.get('egon'):
            print('猜小了!')
        else:
            print('猜大了!')
    
    if __name__ == '__main__':
        main()
    
    
    
    
    [root@Kingstar test]# /usr/bin/python test_3.py 
    please enter the age of egon:18
    猜对了!
    [root@Kingstar test]# /usr/bin/python test_3.py 
    please enter the age of egon:19
    猜大了!
    [root@Kingstar test]# /usr/bin/python test_3.py 
    please enter the age of egon:17
    猜小了!

     第五题


    程序从数据库中取出来10000条数据,打算显示到页面中,
    但一个页面最多显示30条数据,请选取合适的算数运算符,计算
       显示满30条数据的页面总共有多少个?
       最后一页显示几条数据

     代码如下:

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    def main():
        num_of_data = 10000
        per_data = 30
        print(f'显示满30条数据的页面一共有{num_of_data//per_data}')
        print('最后一页显示{0}条数据'.format(num_of_data%per_data))
    
    if __name__ == '__main__':
        main()
    
    
    
    
    
    [root@Kingstar test]# /usr/bin/python test_4.py 
    显示满30条数据的页面一共有333
    最后一页显示10条数据

    第六题


    egon今年为18岁,请用增量赋值计算3年后egon老师的年龄

     代码如下:

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    def main():
        age = 18
        print('3年后,{0}的年龄为{1}'.format('egon',age+3))
    
    if __name__ == '__main__':
        main()
    
    
    
    
    [root@Kingstar test]# /usr/bin/python test_5.py 
    3年后,egon的年龄为21

     第七题


    将值10一次性赋值给变量名x、y、z
    
    >>> x=y=z=10
    >>> print(x,y,z)
    10 10 10
    >>> 

     第八题


    请将下面的值关联到它应该对应的变量名上,你懂的
    dsb = "egon"
    superman = "alex"

    >>> dsb = 'egon'
    >>> superman = 'alex'
    >>> dsb,superman = superman,dsb
    >>> print(dsb,superman)
    alex egon
    >>>

     第九题


    我们只需要将列表中的傻逼解压出来,一次性赋值给对应的变量名即可
    names=['alex_sb','wusir_sb','oldboy_sb','egon_nb','lxx_nb','tank_nb']

    >>> names=['alex_sb','wusir_sb','oldboy_sb','egon_nb','lxx_nb','tank_nb']
    >>> x,y,z,*_=names
    >>> print(x,y,z)
    alex_sb wusir_sb oldboy_sb
    >>>

     附加题:

    编写用户登录接口(学的多的同学,尝试做下述作业,这是截止到下周二学完文件处理之后的作业)
    #1、输入账号密码完成验证,验证通过后输出"登录成功"
    #2、可以登录不同的用户
    #3、同一账号输错三次锁定(附加功能,在程序一直运行的情况下,一旦锁定,则锁定5分钟后自动解锁)
    #扩展需求:在3的基础上,完成用户一旦锁定,无论程序是否关闭,都锁定5分钟

    代码如下:

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    #project:login
    #author:surpass_w
    
    import os
    import sys
    import time
    import subprocess
    from threading import Timer
    
    user_info = [{'account':'egon','password':'success888'},
                 {'account':'alex','password':'ok666abc'},
                 {'account':'surpass','password':'svipaa1'}]
    err_record = [{'account':'egon','record':0},
                  {'account':'alex','record':0},
                  {'account':'surpass','record':0}]
    lock_file = 'block_account.txt'
    
    class Login:
        def __init__(self,user_dict,err_dict,interval):
            self.user_dict = user_dict
            self.err_dict = err_dict
            self.interval = interval
    
        def login(self):
            count = 0
            self.del_lock_account = []
            while True:
                try:
                    f = open(lock_file,'r+')
                except:
                    cmd = 'touch %s
    chmod +x %s' % (lock_file,lock_file)
                    subprocess.check_call(cmd,shell=True)
                    f = open(lock_file,'r+')
                lock_user_list = f.readlines()
                self.lock_account_dict = {lock_user_list[i].replace('
    ','').split(' ')[0]
                                     :lock_user_list[i].replace('
    ','').split(' ')[1]
                                      for i in range(len(lock_user_list))}
                username = input('Please enter your account >>>:')
                if username in self.lock_account_dict.keys():
                    print('The account [%s] has been locked' % username)
                elif username in self.user_dict.keys():
                    password = input('Please enter your password >>>:')
                    if password == self.user_dict.get(username):
                        print('Welcome %s, a successful login!' % username)
                        sys.exit(0)
                    else:
                        self.err_dict[username] += 1
                        if self.err_dict[username] == 3:
                            lock_time = int(time.time())
                            self.del_lock_account.append(username)
                            f.write(username+' '+str(lock_time)+'
    ')
                            self.err_dict[username] = 0
                            print('The account [%s] is about to be locked' % username)
                            self.t = Timer(self.interval,self.update_lock_file)
                            self.t.start()
                        else:
                            print('Wrong password!
    You have 3 chances in all, remain %d.'
                                   %(3-self.err_dict[username]))
                else:
                    count += 1
                    if count == 3:
                        print('Enter invalid account 3 times, the system is shutdown!')
                        sys.exit(0)
                    else:
                        print('The account [%s] does not exist!
    Please reenter, you have 3 chances'+
                               'in all, remain %d.' % (3-count))
                f.close()
    
        def update_lock_file(self):
            for i in range(len(self.del_lock_account)):
                if self.del_lock_account[i] in self.lock_account_dict.keys():
                    local_machine_time = int(time.time())
                    if (int(self.lock_account_dict.get(self.del_lock_account[i]))
                        +300) == local_machine_time:
                        del self.lock_account_dict[self.del_lock_account[i]]
            f1 = open(lock_file,'w')
            for k,v in self.lock_account_dict.items():
                f1.write(k+' '+v+'
    ')
            f1.close()
    
    def main():
        user_dict = {user_info[i].get('account'):user_info[i].get('password')
                      for i in range(len(user_info))}
        err_dict = {err_record[j].get('account'):err_record[j].get('record')
                     for j in range(len(err_record))}
        interval = 300
        l = Login(user_dict,err_dict,interval)
        l.login()
    
    if __name__ == '__main__':
        main()
    View Code
  • 相关阅读:
    java基础-代理模式
    java基础-反射(细节)
    java基础-反射
    设计模式之单例
    23种设计模式汇总整理
    dialog--not attached to window manager
    java之设计模式
    android-sdk和api版本
    studio之mac快捷键
    控件之ReleLayout属性
  • 原文地址:https://www.cnblogs.com/surpass123/p/12423373.html
Copyright © 2011-2022 走看看