第一模块练习题

1  

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

username = "seven"
password = '123'
user_inputname = input("请输入用户名:")
user_inputpassword = input("请输入密码:")
if user_inputname == username and user_inputpassword == password:
    print("登录成功!")
else:
    print("登录失败!")
View Code
username = "seven"
password = '123'
count = 0
while count < 3:
    user_inputname = input("请输入用户名:")
    user_inputpassword = input("请输入密码:")
    if user_inputname == username and user_inputpassword == password:
        print("登录成功!")
        break
    else:
        print("登录失败!")
        count +=1
View Code
username = ["seven","alex"]
password = '123'
count = 0
while count < 3:
    user_inputname = input("请输入用户名:")
    user_inputpassword = input("请输入密码:")
    if user_inputname in username and user_inputpassword == password:
        print("登录成功!")
        break
    else:
        print("登录失败!")
        count +=1
View Code

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

3  使用while 循环输出100-50,从大到小,如100,99,98...,到50时再从0循环输出到50,然后结束

i =2
sum = 0
while i <101:
    if i %2 ==0:
        sum +=i
    else:
        sum -=i
    i +=1
print(sum)
View Code
i =100
while i>50:
    print(i)
    i -=1
    if i == 50:
        i =0
        while i<=50:
            print(i)
            i +=1
        break
View Code

 

4 一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?

m = 0
height = 100
count = 0
while count <=10:
    m += height  # 下落米数
    height = height /2
    m +=height  # 反弹米数
    count +=1
    print(m,height)
View Code

5 编写登陆接口

基础需求:

  • 让用户输入用户名密码
  • 认证成功后显示欢迎信息
  • 输错三次后退出程序

升级需求:

  • 可以支持多个用户登录 (提示,通过列表存多个账户信息)
  • 用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)
f = open("user_data",'r')
accounts = eval(f.read())
count = 0
is_same_user = True
last_username = None
while count <3:
    username = input("Please enter your username:").strip()
    password = input("Please enter your password:").strip()
    if last_username is None:  # 第一次循环
        last_username = username

    if username != last_username:
        is_same_user = False

    if username in accounts:
        if accounts[username][1]==0 :  # 判断用户没有被锁定
            if password == accounts[username][0]:
                print('welcome...')
                break
            else:
                print("wrong username or password.")
        else:
            print('The username has been locked.')
    else:
        print("用户不存在")
    count +=1
else:
    print("Too many attemps...")

    if is_same_user ==True:
        accounts[username][1]=1

        f = open("user_data","w")
        f.write(str(accounts))
        f.close()
print(accounts)
View Code

 

posted @ 2019-10-23 23:22  不再少年  阅读(113)  评论(0编辑  收藏  举报