python for循环

简介:

猜数字游戏:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env   python
# -*- coding:utf-8 -*-
age_of_xcn  =  20
for  in  range ( 3 ):
     guess_age  =  int ( input ( "guess age:" ))
     if  guess_age  = =  age_of_xcn:
         print ( "yes,you got it" )
         break
     elif  guess_age > age_of_xcn:
         print ( "think smaller" )
     else :
         print ( "think bigger" )
else :
     print ( "bay bay" )
  
执行结果:
guess age: 1
think bigger
guess age: 2
think bigger
guess age: 4
think bigger
bay bay
 
进程已结束,退出代码

wKioL1k3pUPRfrlsAABCPSpARck159.png-wh_50


#只打印偶数(10以内)

1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/env   python
# -*- coding:utf-8 -*-
for  in  range ( 0 10 2 ):
     print ( "loop" , i)
     
执行结果:
loop  0
loop  2
loop  4
loop  6
loop  8


猜数字游戏:

当用户输入3后则会提示“do you want to keep ?”输入知道不是n,则count=0,然后重新从0开始循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env   python
# -*- coding:utf-8 -*-
age_of_xcn  =  20
count  =  0
while  count <  3 :
     guess_age  =  int ( input ( "guess age:" ))
     if  guess_age  = =  age_of_xcn:
         print ( "yes,you got it" )
         break
     elif  guess_age > age_of_xcn:
         print ( "think smaller" )
     else :
         print ( "think bigger" )
     count  + =  1
     if  count  = =  3 :
         confirm  =  input ( "do you want to keep ?" )
         if  confirm ! =  'n' :
             count  =  0
 
  执行结果:
  guess age: 1
think bigger
guess age: 2
think bigger
guess age: 3
think bigger
do you want to keep ?ere
guess age:

wKioL1k3rIihsO_PAADPjAVEmRg885.png-wh_50



本文转自 baishuchao 51CTO博客,原文链接:http://blog.51cto.com/baishuchao/1933129

相关文章
|
1月前
|
Python
python用户输入和while循环(四)
python用户输入和while循环(四)
26 1
|
1月前
|
安全 Python
python用户输入和while循环(二)
python用户输入和while循环(二)
17 0
|
1月前
|
Python
在Python中while循环
在Python中while循环
16 1
|
2月前
|
机器人 Java C++
python速成之循环分支结构学习
python速成之循环分支结构学习
37 1
|
1月前
|
Python
python用户输入和while循环(一)
python用户输入和while循环(一)
16 0
|
1月前
|
存储 索引 Python
python用户输入和while循环(五)
python用户输入和while循环(五)
17 0
|
1月前
|
Python
python用户输入和while循环(三)
python用户输入和while循环(三)
19 0
|
1月前
|
Python
Python-循环
Python-循环
19 1
|
1月前
|
存储 算法 索引
python用户输入和while循环(六)
python用户输入和while循环(六)
18 0
|
1月前
|
存储 索引 Python
python用户输入和while循环(七)
python用户输入和while循环(七)
16 0

热门文章

最新文章