python基础之条件和循环(十一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hiwoshixiaoyu/article/details/89321986

if else

格式

if 判断条件:
    执行语句……
else:
    执行语句……
   chePiao = 1 # 用1代表有车票,0代表没有车票
    if chePiao == 1:
        print("有车票,可以上火车")
        print("终于可以见到Ta了,美滋滋~~~")
    else:
        print("没有车票,不能上车")
        print("亲爱的,那就下次见了,一票难求啊~~~~(>_<)~~~~")

if elif elif

格式

if 判断条件1:
    执行语句1……
elif 判断条件2:
    执行语句2……
elif 判断条件3:
    执行语句3……
else:
    执行语句4……
score = 77

    if score>=90 and score<=100:
        print('本次考试,等级为A')
    elif score>=80 and score<90:
        print('本次考试,等级为B')
    elif score>=70 and score<80:
        print('本次考试,等级为C')
    elif score>=60 and score<70:
        print('本次考试,等级为D')
    elif score>=0 and score<60:
        print('本次考试,等级为E')

while循环

格式

while 判断条件:
    执行语句……

    i = 0
    while i<10000:
        print("媳妇儿,我错了")
        i+=1
        

while嵌套循环

   i = 1
    while i<=5:

        j = 1
        while j<=i:
            print("* ",end='')
            j+=1

        print("\n")
        i+=1

for循环

格式

for iterating_var in sequence:
   statements(s)
  name = 'xiaoyu'

    for x in name:
        print(x)

循环控制语句

循环控制语句可以更改语句执行的顺序。Python支持以

循环控制语句

控制语句 描述
break 语句 在语句块执行过程中终止循环,并且跳出整个循环
continue 语句 在语句块执行过程中终止当前循环,跳出该次循环,执行下一次循环。
pass 语句 pass是空语句,是为了保持程序结构的完整性。

- break

 name = 'xiaoyu'

  for x in name:
      print('----')
      if x == 'o': 
          break
      print(x)

- continue

 name = 'xiaoyu'

  for x in name:
      print('----')
      if x == 'o': 
          continue
      print(x)

- pass

#!/usr/bin/python
# -*- coding: UTF-8 -*- 
 
# 输出 Python 的每个字母
for letter in 'Python':
   if letter == 'h':
      pass
      print '这是 pass 块'
   print '当前字母 :', letter
 
print "Good bye!"

当前字母 : P
当前字母 : y
当前字母 : t
这是 pass 块
当前字母 : h
当前字母 : o
当前字母 : n
Good bye!

`

猜你喜欢

转载自blog.csdn.net/hiwoshixiaoyu/article/details/89321986