登陆的循环、判断

1、用 while 循环、for 循环实现ide

  1. username = joseph,passwd = 123456
  2. 让用户输入帐号和密码,输入用户和密码输入正确的话,提示你  xxx,欢迎登陆,今天的日期是xxx,程序结束
  3. 错误的话,提示帐号/密码输入错误,最多输入3次,若是输入3次都没有登陆成功,提示失败次数过多。
  4. 须要判断输入是否为空,输入空也算输入错误一次
1 # 使用 for 循环
 2 import datetime 3 today = datetime.date.today() 4 username = 'joseph'
 5 passwd = 123456
 6 for i in range(3): 7     get_username = input('Please input username:') 8     get_passwd = input('Please input passwd:') 9     if get_username.strip() == str(username).strip(): 10         if get_passwd.strip() == str(passwd).strip(): 11             print('%s,欢迎登陆,今天的日期是%s' %(get_username,today)) 12             break
13         else: 14             print('帐号/密码输入错误') 15     else: 16         print('帐号/密码输入错误') 17 else: 18     print('失败次数过多')
View Code
 1  1 # 使用 While 循环
 2  2 import datetime  3  3 today = datetime.date.today()  4  4 username = 'joseph'
 5  5 passwd = 123456
 6  6 count = 0  7  7 while count < 3:  8  8     get_username = input('Please input username:')  9  9     get_passwd = input('Please input passwd:') 10 10     if get_username.strip() == str(username).strip(): 11 11         if get_passwd.strip() == str(passwd).strip(): 12 12             print('%s,欢迎登陆,今天的日期是%s' %(get_username,today)) 13 13             break
14 14         else: 15 15             print('帐号/密码输入错误') 16 16     else: 17 17         print('帐号/密码输入错误') 18 18     count = count + 1
19 19 else: 20 20     print('失败次数过多')
View Code

 知识点:spa

  • while...else,for...else 的用法
  • .strip() 的用法

2、增长提示“用户名/密码不能为空”code

 1  1 import datetime  2  2 today = datetime.date.today()  3  3 username = 'joseph'
 4  4 passwd = 123456
 5  5 count = 0  6  6 while count < 3:  7  7     get_username = input('Please input username:')  8  8     get_passwd = input('Please input passwd:')  9  9     if get_username.strip() != '' and get_passwd != '': 10 10         if get_username.strip() == str(username).strip(): 11 11             if get_passwd == str(passwd): 12 12                 print('%s,欢迎登陆,今天的日期是%s' %(get_username,today)) 13 13                 break
14 14             else: 15 15                 print('帐号/密码输入错误') 16 16         else: 17 17             print('帐号/密码输入错误') 18 18     else: 19 19         print('用户名/密码不能为空') 20 20     count = count + 1
21 21 else: 22 22     print('失败次数过多')
View Code

 知识点:blog

  • 判断字符串为空:s.strip() == ''
 1 # 增长提示用户名、密码为空
 2 import datetime  3 today = datetime.date.today()  4 username = 'joseph'
 5 passwd = 123456
 6 count = 0  7 while count < 3:  8     get_username = input('Please input username:')  9     get_passwd = input('Please input passwd:') 10     if not get_username and not get_passwd: 11         print('用户名/密码不能为空') 12     elif get_username.strip() == str(username).strip(): 13         if get_passwd == str(passwd): 14             print('%s,欢迎登陆,今天的日期是%s' %(get_username,today)) 15             break
16         else: 17             print('帐号/密码输入错误') 18     else: 19         print('帐号/密码输入错误') 20     count = count + 1
21 else: 22     print('失败次数过多')
View Code

 知识点:ip

  • 非空即真 ,非0即真。
相关文章
相关标签/搜索