zoukankan      html  css  js  c++  java
  • python基础篇03-range/for/break/continue/while/if

    range:

      for i in range(3)    #0 1 2(默认步长为1)

      range(1,5,2)  # 1-4步长为2,即1,3

    range(10,0,-1) #  10-0  倒着取值

    range(10,0,-2) #  10-0  倒着取值,步长为2

     

    if:

      语法:

      if 条件:

        满足条件执行代码

      elif 条件:

        上面的条件不满足执行这个

      elif 条件:

        上面的条件不满足执行这个

      else:

        所有条件都不满足则执行这个

    for循环:in后面必须是可迭代对象,可以是range/列表/元组/字典/字符串/生成器/迭代器 。

    可迭代对象:

    即对象内部有__inter__方法。

     1 #__author:  Administrator
    2 #date:  2016/8/22
    3 _user ="lriwu"
    4 _passwd = "abc123"

    5
    passed_authentication = False #假,不成立 6 for i in range(3): #0 1 2 7 username = input("Username:") 8 password = input("Password:") 9 if username == _user and password == _passwd : 10 print("Welcome %s login...." % _user) 11 passed_authentication = True #,真,成立 12 break #跳出,中断 13 else: 14 print("Invalid username or password !") 15 if not passed_authentication:#只有在True的情况下,条件成立 16 print("账号锁定。")
     #__author:  Administrator
     #date:  2016/8/22
     _user ="lriwu"
     _passwd = "abc123"
     passed_authentication = False  #假,不成立
     for i in range(3):   #0 1 2
          username = input("Username:")
          password = input("Password:")
          if username == _user and password == _passwd :
              print("Welcome %s login...." % _user)
              passed_authentication = True #,真,成立
              break #跳出,中断
          else:
              print("Invalid username or password !")
     else:
         print("账号锁定。")  #只要上面的for循环正常执行完毕,中间没有被打断,就会执行else语句。

     for循环内部三件事:

      1.调用可迭代对象的iter()方法返回一个迭代器对象;(于是就有了next()方法)

      2.不断调用迭代器的next()方法;

      3.处理StopIteration异常。

    continue:

     1 #__author:  Administrator
     2 #date:  2016/8/22
     3 
     4 exit_flag = False
     5 
     6 for i in range(10):
     7     if i <5:
     8         continue   #结束本次循环,进入下次循环
     9     print(i )
    10     for j in range(10):
    11         print("layer2",j)
    12         if j == 6:
    13             exit_flag = True
    14             break   #结束循环
    15     if exit_flag:
    16         break

    while:

     1 #__author:  Administrator
     2 #date:  2016/8/22
     3 _user ="lriwu"
     4 _passwd = "abc123"
     5 counter = 0
     6 while counter <3:
     7     username = input("Username:")
     8     password = input("Password:")
     9     if username == _user and password == _passwd :
    10         print("Welcome %s login...." % _user)
    11         break #跳出,中断
    12     else:
    13         print("Invalid username or password !")
    14     counter += 1
    15     if counter == 3:
    16         keep_going_choice = input("还想玩么?[y/n]")
    17         if keep_going_choice == "y":
    18             counter = 0
    19 
    20 else:
    21     print("账号锁定")
  • 相关阅读:
    redis学习笔记(三)——redis的命令大全总结
    redis学习笔记(二)——java中jedis的简单使用
    redis学习笔记(一)——windows下redis的安装与配置
    快速排序
    SpringMvc实现批量删除,使用post传值一直报404错误
    Bootstrap-table 显示行号
    选择排序
    冒泡排序
    org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class
    Jquery Validate动态添加和删除校验规则
  • 原文地址:https://www.cnblogs.com/lriwu/p/8309772.html
Copyright © 2011-2022 走看看