作业--第一周

版权声明:此博客实属作者原创,转载请注明出处! https://blog.csdn.net/Onion_cy/article/details/82730508

1.简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释型                                                 

编译型:所有代码编译完成之后存放到内存中进行下一步在CPU中的运行,程序没有问题的前提下,下次使用时可以直接使用。优点是执行效率高,缺点是调试程序比较麻烦                                                                                                               

解释型:代码写一行运行一行。优点是调试程序比较方便。缺点是执行效率相对于编译型来说稍低。

2.执行 Python 脚本的两种方式是什么                                                                                                                                             

1:交互式环境 (写入代码立即执行)     2: 代码写入文件(执行xxx.py文件)

3.Pyhton 单行注释和多行注释分别用什么?

执行单行注释时,光标在哪一行,使用快捷键ctrl+?/ 即可执行单行注释

执行多行注释时.使用鼠标选中多行文件,然后使用ctrl+?/  即可实现多行代码的注释

4.布尔值分别有什么?

布尔值只有两个:True  和 False

5.声明变量注意事项有那些?

1.变量名必须是英文字母和数字的组合

2.变量名不可以是字母开头

3.变量名不可以使用关键字命名

3.变量的命名风格有两种风格:驼峰体(AgeOfallen)和下划线纯小写(age_of_allen,python中一般使用这种类型)两种风格

6.如何查看变量在内存中的地址?

使用id指令来查看变量在内存中的地址映射

例如查看变量A在内存中的映射:print(id(A))

7.写代码

实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败!

name ='seven'
pwd ='123'
while True:
    name=input('please input your name>>:')
    pwd=input('please input your pwd>>:')
    if name=='seven' and pwd=='123':
        print('恭喜你,登陆成功')
        break
    else:
        print("账户名或密码输入错误,登录失败!")
       
       

实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次

name ='seven'
pwd ='123'
count=0
while count<3:
    name=input('please input your name>>:')
    pwd=input('please input your pwd>>:')
    if name=='seven' and pwd=='123':
        print('恭喜你,登陆成功')
        break
    else:
        print("账户名或密码输入错误,登录失败!")
        # count+=1
        print('你已经尝试的次数:',count)
else:
    count = 3
    print('对不起,你尝试次数过多,即将退出程序...')

print('END')

实现用户输入用户名和密码,当用户名为 seven 或 alex 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次

name ='seven' or 'alex'
pwd ='123'
count=0
while count<3:
    name=input('please input your name>>:')
    pwd=input('please input your pwd>>:')
    if name=='seven'or 'alex' and pwd=='123':
        print('恭喜你,登陆成功')
        break
    else:
        print("账户名或密码输入错误,登录失败!")
        # count+=1
        print('你已经尝试的次数:',count)
else:
    count = 3
    print('对不起,你尝试次数过多,即将退出程序...')

print('END')

8.写代码


a. 使用while循环实现输出2-3+4-5+6...+100 的和

while count<=100:
    if count%2==0:
        sum+=count
        count += 1
    else:
        sum-=count
        count+=1
print(sum)


b. 使用 while 循环实现输出 1,2,3,4,5, 7,8,9, 11,12 使用 while 循环实现输出 1-100 内的所有奇数

1.使用while循环输出123456789101112

L=1
while L<12:
    if L==6 or L==10:
        L+=1
        continue
    print(L)
    L+=1

2使用while循环实现输出1-100内的所有奇数

l=0
while l<100:
    if l%2==1:
        l+=1
    else:
        l+=1
        print(l)

3. 使用 while 循环实现输出 1-100 内的所有偶数

l=0
while l<100:
    if l%2==0:
        l+=1
    else:
        l+=1
        print(l)

9.现有如下两个变量,请简述 n1 和 n2 是什么关系?

  n1 = 123456
      n2 = n1

n1和n2的值相同,内存地址在pycharm也是相同的  在交互式环境中是不同的

10.让用户输入用户名密码

认证成功后显示欢迎信息

输错三次后退出程序

name='allen'
pwd='123'
count=0
while count<=3:
    if count==3:
        print('您输入的次数过多,即将退出程序///')
        break
    username=input('请输入你的用户名>>:')
    password=input('请输入你的登陆密码>>:')
    if username==name and pwd==password:
        print("登陆成功")
        break
    else:
        print('你输入的用户名或者账户有误,请重新输入')
        count+=1

升级需求:

可以支持多个用户登录 (提示,通过列表存多个账户信息)

用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)

dic={
    'allen':{'password':'123','count':0},
    'michale':{'password':'123','count':0},
    'maria':{'password':'123','count':0},
}
count=0
while True:
    name=input('请输入您的用户名>>: ')
    if name not in dic:
        print('用户不存在')
        continue

    with open('lock.txt','r') as f:
        lock_users=f.read().split('|')
        if name  in lock_users:
            print('用户%s已经被锁定' %name)
            break

    if dic[name]['count'] > 2:
        print('尝试次数过多,您的用户将被锁定')
        with open('lock.txt','a') as f:
            f.write('%s|' %name)
        break

    password=input('请输入您的密码>>: ')

    if password == dic[name]:
        print('登录成功')
        break
    else:
        print('您输入的用户名或密码错误')
        dic[name]['count']+=1

猜你喜欢

转载自blog.csdn.net/Onion_cy/article/details/82730508