import getpass name = input(‘Please input your name:‘) pwd = getpass.getpass(‘Please input your password:‘) if name == "alex" and pwd == "cmd": print(‘Welcome, Alex!‘) else: print(‘User name or password is wrong! Please retry.‘)
# test <continue> count = 1 while count < 10: if count == 7: count += 1 continue print(count) count += 1
代码中,continue结束当前循环,进入下一次循环。
与此对比,break则结束全部循环,进入循环后面的代码。
# test <break> count = 1 while count < 10: if count == 7: count += 1 break print(count) count += 1
上面程序输出1-6,当count=7时,则退出循环。
# user login with three trying times import getpass count = 0 while count < 3: name = input(‘Please input your name:‘) pwd = getpass.getpass(‘Please input your password:‘) if name == "alex" and pwd == "cmd": print(‘Welcome, Alex!‘) break else: print(‘User name or password is wrong! Please retry.‘) count += 1 print(‘next options...‘)
成员操作符:in 与 not in命令,判断一个字符串是否为另一个字符串的子集。
# in and not in name = "alexprone" if "alx" in name: print(‘OK‘) elif "alx" not in name: print(‘good‘) else: print(‘Error‘)
布尔值一共有两个值:
True:真
False:假
+, -, *, /, %, **, //;
=,+=, -=, *=, /=, %=, **=, //=
==, >, <, >=, <=, !=(不等于), <>(不等于),
and(与运算符),or(或运算符),not(取反操作符);
in, not in
先计算括号内,从前往后计算,分类讨论:
True or ==> True
True and ==> go on
False or ==> go on
False and==> False
原文:https://www.cnblogs.com/yangjingxuan/p/11666760.html