Python学习心得第一周-03练习2

#5. 求1-2+3-4+5 ... 99的所有数的和
res=0
count=1
while count <100:
    if count%2 == 0:
        res-=count
    else:
        res+=count
    count+=1
print(res)
#6. 用户登陆(三次机会重试)
count=0
while count<3:
    name=input('name:')
    password=input('password:')
    if name=='ztc' and password=='sunmi':
        print('OK')
        break
    else:
        print('Fail')
        count+=1
        continue
# #7:猜年龄-游戏要求:允许用户最多尝试3次,3次都没猜对的话,就直接退出
# # 如果猜对了,打印恭喜信息并退出
age=39
count=0
while count<3:
    inp_age=int(input('内容:'))
    if inp_age==age:
        print('恭喜你,猜对了')
        break
    elif inp_age<age:
        print('猜小了')
    else:
        print('猜大了')
    count+=1
# #8:猜年龄游戏升级版
# '''要求:
# 允许用户最多尝试3次
# 每尝试3次后,如果还没猜对,就问用户是否还想继续玩,
# 如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
# 如何猜对了,就直接退出 '''
age=65
count=0
while True:
    if count == 3:
        choice=input('继续(Y/N?)>>: ')
        if choice == 'Y' or choice == 'y':
            count=0
        else:
            break
    inp_age=int(input('输入内容:'))
    if inp_age==age:
        print('you are right!')
        break
    count+=1
#练习1:实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败!
username=input('请输入用户名:')
password=input('请输入密码:')
if username=='seven' and password=='123':
    print('登陆成功')
else:
    print('登录失败,用户名或密码错误')
#练习2:实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次
#
name = 'seven'
pwd = '123'
count = 0
while count < 3:
    inp_name = input('请输入用户名:')
    inp_pwd = input('请输入密码:')
    if inp_name == name and inp_pwd  == pwd:
        print('登录成功')
        break
    else:
        print('登录失败')
        count+=1

#
count = 0
while count < 3:
    name=input('请输入用户名:')
    pwd=input('请输入密码:')
    if name=='seven' and pwd=='123':
        print('登录成功')
        break
    else:
        print('登录失败')
        count+=1
##实现用户输入用户名和密码,当用户名为 seven 或 alex 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次
count = 0
while count < 3:
    name=input('请输入用户名:')
    pwd=input('请输入密码:')
    if name=='seven' and pwd=='123':
        print('登录成功')
        break
    elif name=='alex' and pwd=='123':
        print('登录成功')
        break
    else:
        print('登录失败')
        count+=1
原文地址:https://www.cnblogs.com/zhutiancheng/p/9229334.html