Python3作业-登录程序

要求:

写一个登录程序,定义好username、passwd。

让用户输入账号和密码。输入用户和密码后判断,输入正确的话 提示:xxx,欢迎登录,今天的日期是xxx,且程序结束;输入错误的话,提示:账号/密码输入错误;最多可输入3次,如果输入3次都没有登录成功,提示:失败次数过多;输入的内容需要判断输入是否为空

需求分析:

1、输入内容可用input方法

2、判断内容是否正确,需要用 if ...elif ...els ...条件判断

3、最多输入三次,需要用循环。

循环有while 、for 两种,先看看while循环的写法


# 第一种写法,while循环
import datetime
today = datetime.date.today()
username = 'fengfeng'
passwd = '123456'
count = 0
while count < 3:
    count += 1
    input_username = input('登录名:')
    input_passwd = input('密码:')
    if input_username.strip() == '' or input_passwd.strip() == '':
        print('账号/密码不能为空')
        continue
    elif input_username != username or input_passwd != passwd:
        print('账号/密码输入错误')
    else:
        print('%s ,欢迎登录,今天的日期是%s'%(username,today))
        break
else:
    print ('失败次数过多')

下面再看看for循环的写法:

# 第二种写法,for循环
import datetime
today = datetime.date.today()
username = 'fengfeng'
passwd = '123456'
for count in range(3):
    input_username = input('登录名:')
    input_passwd = input('密码:')
    if input_username.strip() == '' or input_passwd.strip() == '':
        print('账号/密码不能为空')
    elif input_username != username or input_passwd != passwd:
        print('账号/密码输入错误')
    else:
        print('%s ,欢迎登录,今天的日期是%s'%(username,today))
        break
else:
    print ('失败次数过多')
原文地址:https://www.cnblogs.com/fenggf/p/8781666.html