首页 > 编程语言 > 详细

python学习2

时间:2019-10-13 16:23:38      阅读:89      评论:0      收藏:0      [点我收藏+]

python学习2

用户名密码处理方式:

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.)

输出1-9,不输出7的另一个方法:

# test <continue>
count = 1
while count < 10:
    if count == 7:
        count += 1
        continue
    print(count)
    count += 1

代码中,continue结束当前循环,进入下一次循环。

与此对比,break则结束全部循环,进入循环后面的代码。

测试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

python学习2

原文:https://www.cnblogs.com/yangjingxuan/p/11666760.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!