Linux:python控制语句(if条件语句、while语循环句和退出值)
程序员文章站
2022-03-15 23:19:54
...
一·if语句
# _*_ coding:utf-8 _*_
"""
file: if语句.py
date: 2018-07-17 20:48
author: jiong
desc:
"""
# 1.判断年龄是否为18周岁
age = 20
# 2.如果满18周岁,可以进入网吧
if age >=18:
print '可以进入网吧'
# 3.如果不满18周岁,拒绝进入网吧
else:
print '拒绝进入网吧'
二·逻辑运算
# _*_ coding:utf-8 _*_
"""
file: 逻辑运算.py
date: 2018-07-17 20:54
author: jiong
desc:
"""
# 1.一个整数变量,要求身高在160-170之间
height = 165
if height > 160 and height < 170:
print '身高相符'
else:
print '身高不符'
# 2.两个整数变量,只要求一门成绩>60,即为及格
score1 = 20
score2 = 8
if score1 > 60 or score2 > 60:
print '考试及格'
else:
print '考试不及格'
# 3.判断小明是否是男生
gender = False
if not gender:
print '小明为男生'
# 3.判断小明是否是男生
gender = True
if gender:
print '小明为男生'
三·if嵌套
# _*_ coding:utf-8 _*_
"""
file: if嵌套.py
date: 2018-07-17 20:59
author: jiong
desc:
"""
# 定义布尔型变量has_ticket表示是否有车票
has_ticket = True
# 定义刀的长度
knife_length = 4
# 是否有车票.若有,允许进入
if has_ticket:
print '请进入'
# 判断刀的长度
if knife_length > 5:
print '长度超过限定长度,不允许进入'
else:
print '长度未超过限定长度,允许进入'
else:
print '请先购买车票'
四·if综合案例(以色子为例)
# _*_ coding:utf-8 _*_
"""
file: if案例_色子.py
date: 2018-07-17 21:02
author: jiong
desc:
"""
import random
# 1.从控制台输入筛子:1,2,3,4,5,6
player = int(raw_input('请输入筛子数:'))
# 2.电脑输出筛子数:1,2,3,4,5,6
computer = random.randint(1,6)
print '玩家输入的是 %d 电脑输入的是 %d' % (player,computer)
# 3.判断
if (( player == 6 and computer <= 5 ) or
( player == 5 and computer <= 4 ) or
( player == 4 and computer <= 3 ) or
( player == 3 and computer <= 2 ) or
( player == 2 and computer <= 1 )):
print '玩家胜利'
elif player == computer:
print '平局'
else:
print '电脑胜利'
五·while语句
1、简单while语句
# _*_ coding:utf-8 _*_
"""
file: while_1.py
date: 2018-07-17 21:04
author: jiong
desc:
"""
# 定义一个整数变量,记录循环的次数
i = 0
# 开始循环
while i<= 3:
# 希望在循环内执行的代码
print 'hello python'
# 处理循环计数
i += 1
print '循环结束,i = %d' % i
2、计算0~100之间数字的求和
# _*_ coding:utf-8 _*_
"""
file: while_2.py
date: 2018-07-17 21:10
author: jiong
desc:
"""
# 计算0~100之间数字的求和
i = 0
result = 0
while i <= 100:
print i
result += i
i += 1
print '0~100之间的数字求和的结果是: %d' % result
3、计算0~100之间奇数的和
# _*_ coding:utf-8 _*_
"""
file: while_3.py
date: 2018-07-17 21:11
author: jiong
desc:
"""
# 计算0~100之间奇数的和
i = 1
result = 0
while i <= 100:
print i
result += i
i += 2
print '0~100之间奇数的和结果是: %d' % result
4、运行结果为2-3+4-5+6-7+8…+100
# _*_ coding:utf-8 _*_
"""
file: while_4.py
date: 2018-07-17 21:14
author: jiong
desc:
"""
i = 2
b = 0
while i <= 100:
if i % 2 == 0:
b = b + i
else:
b = b - i
i += 1
print b
六·退出值
1、break
# _*_ coding:utf-8 _*_
"""
file: 退出值_break.py
date: 2018-07-17 21:16
author: jiong
desc:
"""
i = 0
while i < 10:
if i == 3:
# 满足某一条件时,退出循环,不再执行后续的重复的代码
break
print i
i += 1
print 'over'
2、continue
# _*_ coding:utf-8 _*_
"""
file: 退出值_continue.py
date: 2018-07-17 21:18
author: jiong
desc:
"""
i = 0
while i <10:
i += 1
if i == 3:
# 满足某一条件时
# 不执行后续重复的代码
# 其他条件的时候都要执行
continue
print i
七·while嵌套
1、显示出以下形式
*
* *
* * *
* * * *
* * * * *
方法一:
# _*_ coding:utf-8 _*_
"""
file: while嵌套.py
date: 2018-07-17 21:22
author: jiong
desc:
"""
"""
*
* *
* * *
* * * *
* * * * *
"""
row = 1
while row <= 5:
print '* ' * row
row += 1
方法二:
row = 1
while row <=5:
col = 1
while col <= row:
col += 1
print '*',
#print '第 %d 行' %row
print ''
row += 1
2、嵌套循环(九九乘法表)
# _*_ coding:utf-8 _*_
"""
file: 嵌套循环_九九乘法表.py
date: 2018-07-17 21:29
author: jiong
desc:
"""
row = 1
while row <= 9:
col = 1
while col <= row:
print '%d * %d = %d\t' % (row ,col,col * row),
col += 1
print ''
row += 1
转移字符
\t:在控制台输入一个制表符,协助在输出文本的时候垂直方向保持对其
\n:换行符