python基础查漏补缺2--编写程序&流程控制

1. 获得输入内容 input

>>> age = input('how old are you?')
how old are you?10
>>> age
'10'

2. print方法的使用,增加间隔,使用sep,换行方式为end

>>> print('jack','ate','no','fat',sep='.',end='')
jack.ate.no.fat

# test_print.py
print('jack ate', end = '')
print('no fat')

result:  jack ateno fat

3. 对结果求值 eval

>>> eval(input('? '))
?  3 + 4 * 5
23

4. break 和 countinue的使用

break: 结束循环

i = 0
while i < 5:
    i=i+1
    if i == 3:
        break
    print(i, end= ' ')

resut:1 2

continue:跳过当前循环进入下一次迭代

i = 0
while i < 5:
    i=i+1
    if i == 3:
        continue
    print(i, end= ' ')

result: 1 2 4 5 

如果是嵌套循环,则break和continue分别只针对当前循环内容

for row in range(1, 10):
    for col in range(1, 10):
        prod = row * col
        if prod < 10:
            print('  ', end = '')
        print(row * col, '  ', end= '')
    print()

result:

  1     2     3     4     5     6     7     8     9   
  2     4     6     8   10   12   14   16   18   
  3     6     9   12   15   18   21   24   27   
  4     8   12   16   20   24   28   32   36   
  5   10   15   20   25   30   35   40   45   
  6   12   18   24   30   36   42   48   54   
  7   14   21   28   35   42   49   56   63   
  8   16   24   32   40   48   56   64   72   
  9   18   27   36   45   54   63   72   81   

break:

for row in range(1, 10):
    for col in range(1, 10):
        prod = row * col
        if col == 5:
            break
        if prod < 10:
            print('  ', end = '')
        print(row * col, '  ', end= '')
    print()
    

result:
  1     2     3     4   
  2     4     6     8   
  3     6     9   12   
  4     8   12   16   
  5   10   15   20   
  6   12   18   24   
  7   14   21   28   
  8   16   24   32   
  9   18   27   36   

contiune

for row in range(1, 10):
    for col in range(1, 10):
        prod = row * col
        if col == 5:
            continue
        if prod < 10:
            print('  ', end = '')
        print(row * col, '  ', end= '')
    print()

result:
  1     2     3     4     6     7     8     9   
  2     4     6     8   12   14   16   18   
  3     6     9   12   18   21   24   27   
  4     8   12   16   24   28   32   36   
  5   10   15   20   30   35   40   45   
  6   12   18   24   36   42   48   54   
  7   14   21   28   42   49   56   63   
  8   16   24   32   48   56   64   72   
  9   18   27   36   54   63   72   81   

 

posted @ 2017-09-11 18:12  燃灯胡同  阅读(187)  评论(0编辑  收藏  举报