Python的控制语句

 

IF条件语句

  1. 基本if条件语句 

 if condition:
        code block
        code block
    else:
        code block

  2. 嵌套if条件语句

if condition:
    if condition:
        code block
        code block
    else:
        code block
else:
    code block
    code block

  3. elif多条件语句

if condition:
    code block
    code block
elif condition:
    code block
    code block
elif condition
    code block
    code block
else:
    code block 

  4. if多条件语句pass的用法

if condition:
    pass
else:
    code block
print("xxxxxx")

while循环语句

  1. 死循环代码

 

import time
while a == a:
    print("ok",time.time())

 

  2. 普通循环

--------------------------------------------------------
count = 0
while count < 20:
    print(count)
    count = count + 1
---------------------------------------------------------
带else的while循环,条件不成立的时候执行else,只执行一次。
count = 0
while count < 10:
  print(count)
  count = count + 1
else:
  print("else")
---------------------------------------------------------
continue解释:
遇到continue后while循环会立刻跳到while的开始处执行,即continue是终止while的当前次的循环;
count的用法举例:(输出除3以外的1到10的所有数字)
count = 0
while count < 10:
        if count == 3:
                count =  count + 1
                continue
        print(count)
        count = count + 1
---------------------------------------------------------
break解释:
遇到break立刻跳出while循环,即break是终止当前while的所有循环;
break、while、if综合运用举例(用户登录账号和密码,只允许用户尝试3次,第4次输入锁定账号):
# -*- conding: utf-8 -*-
StrUserName = "0"
StrPassword = "0"
StrTest = "hello"
StrTest2 = "123456"
A = 0
while StrUserName != StrTest or StrPassword != StrTest2:
        A = A + 1
        if A == 4:
                print("账号锁定")
                break
        else:
                StrUserName = input("请输入用户名:")
                StrPassword = input("请输入密码:")
                if StrUserName == StrTest and StrPassword == StrTest2:
                        print("欢迎登录")
                else:
                        print("账号或密码错误")
三次登录优化后代码:
# -*- conding: utf-8 -*-
count = 0
while count < 3:
        UserName = input("请输入用户名:")
        Password = input("请输入密码:")
        if UserName == "hello" and Password == "123456":
                print("欢迎登录")
                break
        else:
                if count == 0:
                        print("账号密码错误")
                        print("还剩两次尝试机会")
                elif count == 1:
                        print("账号密码错误")
                        print("还剩一次尝试机会")
                else:
                        print("账号锁定")
        count = count + 1

 in和not in

用来检查连续字符或子序列是否在某个字符串(字典、列表)中存在,举例如下:

#!/user/bin/env python
# -*- conding:utf-8 -*-
haha = "张三赵四王五李六"
count = 0
while count < 3:
    hehe = input("请输入用户名:")
    if  hehe in haha:
        print("用户存在")
    else:
        print("用户不存在")
    count = count + 1
#!/user/bin/env python
# -*- conding:utf-8 -*-
haha = "张三赵四王五李六"
count = 0
while count < 3:
    hehe = input("请输入用户名:")
    if  hehe not in haha:
        print("用户不存在")
    else:
        print("用户存在")
    count = count + 1

 

for循环

#!/usr/bin/env python
# -*- conding: utf-8 -*-
test = "你是风而我是沙缠缠绵绵绕天涯"
for i in test:
    print(i)

 

 

posted @ 2019-12-13 10:07  IamJet  阅读(264)  评论(0编辑  收藏  举报