博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python流程处理
阅读量:7072 次
发布时间:2019-06-28

本文共 2518 字,大约阅读时间需要 8 分钟。

python流程处理

1.流程处理if...else

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash/env python
#_*_ coding:utf-8 _*_
#python version :python3.6
 
import 
getpass
n1 
= 
"abc"
p1 
= 
"123"
 
name 
= 
input
(
"input your name:"
)
passwd 
= 
getpass.getpass(
"input your passwd:"
)
if 
name 
=
= 
n1 
and 
passwd 
=
= 
p1:
    
print
(
"welecome login !"
)
else
:
    
print
(
"user or pass is incorrect"
)

2.流程处理if...elif...elif...else

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash/env python
#_*_ coding:utf-8 _*_
#python version:3.6
n1 
= 
21
 
name 
= 
int
(
input
(
"please input the number which you want to:"
))
if 
name 
=
= 
n1:
    
print
(
"you are too smart!"
)
elif 
name < 
21
:
    
print
(
"is too small!"
)
else
:
    
print
(
"it is too big!"
)

3.for循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1
# 循环10个数字
= 
1
for 
in 
range
(
10
):
    
print
(i)
2
#猜年龄优化for...break
#可以输入10次机会,输入正确就退出。
n1 
= 
21
for 
in 
range
(
10
):
    
name 
= 
int
(
input
(
"please input the number which you want to:"
))
    
if 
name 
=
= 
n1:
        
print
(
"you are too smart!"
)
        
break
;
    
elif 
name < 
21
:
        
print
(
"is too small!"
)
    
else
:
        
print
(
"it is too big!"
)

4.while循环

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash/env python
#_*_ coding:utf-8 _*_
#python version:3.6
 
1
#循环数字,当循环10的时候退出
count 
= 
0
#设置计数器
while 
True
:
    
print
(
"print:" 
,count)
    
count 
+
=
1
    
if 
count 
=
= 
10
:
        
print
(
"loop completed"
)
        
break
;
        
#跳出循环
 
2
#猜年龄游戏,可以输入10次,但是输错3次就退出。
#测试是否可以输入10次,把count < 3 改成count <10 即可;
n1 
= 
21
count 
= 
0
while 
True
:
    
if 
count 
=
= 
10
:
        
print
(
"say goodbye!"
)
    
else
:
        
if 
count < 
3
:
            
name 
= 
int
(
input
(
"please input the number which you want to:"
))
            
if 
name 
=
= 
n1:
                
print
(
"you are too smart!"
)
                
break
;
            
elif 
name < 
21
:
                
print
(
"is too small!"
)
            
else
:
                
print
(
"it is too big!"
)
        
else
:
            
print
(
"you are too failed!"
)
            
break
;
    
count 
+
= 
1
  
3
#猜年龄游戏,可以输入10次,但是输错3次,就再问一下你还要继续吗?不继续就退出。
n1 
= 
21
count 
= 
0
for 
in 
range
(
13
):
    
if 
count < 
3
:
        
name 
= 
int
(
input
(
"please input the number which you want to:"
))
        
if 
name 
=
= 
n1:
            
print
(
"you are too smart!"
)
            
break
;
        
elif 
name < 
21
:
            
print
(
"is too small!"
)
        
else
:
            
print
(
"it is too big!"
)
    
else
:
        
print
(
"you are too failed!"
)
        
input1 
= 
input
(
"Do you want to continue? (y/n)"
)
        
if 
input1 
=
= 
"y"
:
            
count 
= 
0 
;
            
continue
;
        
else
:
            
print
(
"say goodbye!"
)
        
break
;
    
count 
+
= 
1
     
     
注意:counter 
+
=
1 
相等于 counter 
= 
counter 
+
1
     本文转自506554897 51CTO博客,原文链接:http://blog.51cto.com/506554897/1906437
,如需转载请自行联系原作者
你可能感兴趣的文章
合并多个工作薄workbooks到一个工作薄workbook
查看>>
公司的一个面试题:如何用css让一个容器水平垂直居中?
查看>>
Linux概念架构的理解(转)
查看>>
.Net 转战 Android 4.4 日常笔记目录
查看>>
Xamarin体验:使用C#开发iOS/Android应用
查看>>
GitLab版本管理(转)
查看>>
SpringMVC源码解析- HandlerAdapter - ModelFactory(转)
查看>>
[20171127]dual.txt
查看>>
JCombobox组合框效果实现(转)
查看>>
提取重复代码不应该只从代码角度,可以从业务角度看看(转)
查看>>
Spring Web工程web.xml零配置即使用Java Config + Annotation
查看>>
英语术语
查看>>
《Linux From Scratch》第三部分:构建LFS系统 第六章:安装基本的系统软件- 6.58. Kbd-2.0.2...
查看>>
开发 React Native 前必须知道的几件事
查看>>
Spring Cloud构建微服务架构:服务网关(路由配置)【Dalston版】
查看>>
阿里Q2财报:阿里云季度营收达30亿,领先优势持续扩大
查看>>
聚集索引,非聚集索引,唯一索引,索引视图
查看>>
云计算不敷物联网时代所需 “雾计算”应运而生
查看>>
金融安全资讯精选 2017年第十一期 银行木马利用VMvare进行传播 研究人员发现新型安卓银行木马Red Alert...
查看>>
路由器长期开着可以吗?不关有什么危害吗?
查看>>