一个程序学会python的流程控制

song@ubuntu:~$ vi ex4.py
song@ubuntu:~$ more ex4.py
#!/usr/bin/env python
#_*_ coding:utf-8 _*_
print_num=input('Which loop do you want to be printed out?:')
count=0
while count<30:
    if count==print_num:
        print 'There you have got the number:',count
        choice=raw_input('Do you want to continue the loop?(y/n):')
        if choice=='n':
            print 'The while loop is broken by break!'
            break
        else:
            print_num=input('Which loop do you want to be printed out?:')
            while print_num<=count:
                print '%d has gone,please enter another number!' % print_num
                print_num=input('Which loop do you want to be printed out?:')
    else:
        print 'Loop:',count
    count+=1
else:
    print 'The while loop is over normally!'
song@ubuntu:~$
song@ubuntu:~$ python ex4.py
Which loop do you want to be printed out?:3
Loop: 0
Loop: 1
Loop: 2
There you have got the number: 3
Do you want to continue the loop?(y/n):n
The while loop is broken by break!
song@ubuntu:~$
song@ubuntu:~$ python ex4.py
Which loop do you want to be printed out?:19
Loop: 0
Loop: 1
Loop: 2
Loop: 3
Loop: 4
Loop: 5
Loop: 6
Loop: 7
Loop: 8
Loop: 9
Loop: 10
Loop: 11
Loop: 12
Loop: 13
Loop: 14
Loop: 15
Loop: 16
Loop: 17
Loop: 18
There you have got the number: 19
Do you want to continue the loop?(y/n):y
Which loop do you want to be printed out?:16
16 has gone,please enter another number!
Which loop do you want to be printed out?:19
19 has gone,please enter another number!
Which loop do you want to be printed out?:28
Loop: 20
Loop: 21
Loop: 22
Loop: 23
Loop: 24
Loop: 25
Loop: 26
Loop: 27
There you have got the number: 28
Do you want to continue the loop?(y/n):y
Which loop do you want to be printed out?:30
Loop: 29
The while loop is over normally!
song@ubuntu:~$ 

你可能感兴趣的:(python,python,if,else,while,break)