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

来源:互联网 发布:山东黄金历史交易数据 编辑:程序博客网 时间:2024/06/13 23:19
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:~$ 
1 0
原创粉丝点击