流程控制--if条件

/* if ....else .... */
[[email protected] test1]# vim 5.py
//ADD
#!/usr/bin/python

if 1>2:
    print ‘hello python‘
    print ‘TRUE‘
else:
    print ‘FALSE‘

[[email protected] test1]# python 5.py
FALSE
/* elif
如果if 不成立
     elif 成立,则执行elif

如果if不成立
     elif也不成立
      则执行else
*/
[[email protected] test1]# vim 5.py
//ADD
#!/usr/bin/python

if 1>2:
    print ‘hello python‘
    print ‘TRUE‘
elif ‘a‘:
    print ‘b‘
else:
    print ‘FALSE‘

[[email protected] test1]# python 5.py
b
[[email protected] test1]# vim 5.py
//ADD
#!/usr/bin/python

if 1>2:
    print ‘hello python‘
    print ‘TRUE‘
elif 0:
    print ‘b‘
else:
    print ‘FALSE‘

[[email protected] test1]# python 5.py
FALSE
/*     利用 raw_input()输入或输出的是 字符串,

        始终 比整型数值大,所以当设计一个“分数脚本”时,没办法得出正确的结果

        可以利用 int(‘‘)这样的形式,将字符串转换为整型。
*/
[[email protected] test1]# vim 6.py
//ADD
#!/usr/bin/python

score = int(raw_input("Please input your score: "))
if score >= 90:
     print ‘A‘
     print ‘excellent‘
elif score >= 80:
     print ‘B‘
     print ‘very good‘

elif score >=70:
     print ‘C‘
     print ‘good‘
else:
     print ‘D‘
     print ‘Sorry,you failed.‘

[[email protected] test1]# python 6.py
Please input your score: 30
D
Sorry,you failed.
[[email protected] test1]# python 6.py
Please input your score: 70
C
good
[[email protected] test1]# python 6.py
Please input your score: 92
A
excellent
[[email protected] test1]# python 6.py
Please input your score: 83
B
very good
/* a.lower()  -- 这个lower()函数能够把大写的东西变成小写 */
[[email protected] test1]# vim 7.py
//ADD
#!/usr/bin/python

yn = raw_input("Please input [Yes/No]: ")
yn = yn.lower()
if yn == ‘y‘ or yn == ‘yes‘:
    print "programe is running..."
elif yn == ‘n‘ or yn == ‘no‘:
    print "programe is exit"
else:
    print "please input [Yes/No]"

[[email protected] test1]# python 7.py
Please input [Yes/No]: Yes
programe is running...
[[email protected] test1]# python 7.py
Please input [Yes/No]: No
programe is exit
[[email protected] test1]# python 7.py
Please input [Yes/No]: yes
programe is running...
[[email protected] test1]# python 7.py
Please input [Yes/No]: no
programe is exit
[[email protected] test1]# python 7.py
Please input [Yes/No]: ddd
please input [Yes/No]
时间: 2024-08-03 16:15:51

流程控制--if条件的相关文章

Linux shell 流程控制(条件if,循环for,while)

Linux shell 流程控制(条件if,循环[for,while],选择[case]语句实例 linux shell有一套自己的流程控制语句,其中包括条件语句(if),循环语句(for,while),选择语句(case).下面我将通过例子介绍下,各个语句使用方法. 一.shell条件语句(if用法) if语句结构[if/then/elif/else/fi] if 条件测试语句 then action [elif 条件 action else action ] fi 如果对于:条件测试语句不是

python 流程控制(条件语句)

1,python流程控制单条件基本语句 2,python流程流程多条件控制语句 1,python流程控制单条件基本语句 if 判断条件: 执行语句-- else: 执行语句-- 判断条件为真,即执行语句,否则执行else中的语句.变量非空或者不为0或者不为None即为真. 例子: #!/usr/bin/env python # -*- coding: UTF-8 -*- n = input('please input number:') if n > 0: print 'n为正数' else:

java基础 流程控制和条件语句,循环语句

顺序结构 程序的顺序结构: 如果代码里没有流程控制,程序是按照书写的格式从上而下一行一行执行的, 一条语句执行完之后继续执行下一条语句,中间没有判断和跳转,直到程序的结束. if语句 if语句使用boolean表达式或boolean值作为选择条件,有三种结构形式: if翻译成中文,表示如果......,就干......., 第一种结构形式: if(boolean表达式) { 条件执行体 } if后面跟的{}表示一个整体-代码块,我们在这称为条件执行体,也就是说条件为true,就执行这一块代码块.

shell编程 之 流程控制(条件语句和循环语句)

1 if ...else... 基本格式: if condition then commend else commend fi 当然也可以写到一行,用[ ]表明边界,用:表示分行.比如: if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi    判断条件用方括号括起来表示分界,意思是统计当下进程中 带'ssh'的字符串数量,如果这个数量大于1,那就输出true,结束. 还有带elif的格式,如下:

廖雪峰Java1-3流程控制-3条件判断

1.if条件判断的格式 if (条件) { 代码块 } if (条件) { 代码块1 } else { 代码块2 } if (条件1) { 代码块1 } else if { 代码块2 } else { 代码块3 } 2.整型判断 条件判断注意的事项: 注意判断顺序 注意边界条件 int n = 100; if (n >= 90){ System.out.println("优秀"); }else if(n >= 60){ System.out.println("及格

Python流程控制-2 条件判断

条件判断 条件判断是通过一条或多条判断语句的执行结果(True或者False)来决定执行的代码块. 在Python语法中,使用if.elif和else三个关键字来进行条件判断. if语句的一般形式如下所示 if condition1: # condition1为True 执行statement_block_1 statement_block_1 elif condition2: # condition2为True 执行statement_block_2 statement_block_2 els

Perl入门(二)Perl的流程控制

Perl是一种粘性语言,如果你有其他语言的基础的话,你会发现他的流程控制完全和你所知的一模一样. 简单说一下他们的区别: Perl的elsif在其他语言里头可能表示为else if Perl的last,next在其他语言里头可能叫break和continue 不用找了,Perl中没有switch,其实这用if else也是可以实现的,不是么? Perl可能比你知道的语言里头多了这些:条件控制结构unless.循环控制结构until Perl还有一些特殊的写法 还是详细介绍一下Perl的流程控制吧

基础知识回顾——流程控制

通过条件语句或循环语句改变程序运行顺序的过程叫流程控制. 条件语句 条件语句:用于改变程序的执行流程,其中else代码块是可选的.1.if/else 1 pwd = raw_input("what's the password ?") 2 if pwd == 'apple': 3 print "loging on..." 4 else: 5 print "password error!" 6 print "all done"

day6 流程控制 while循环 运算符

具体知识戳这里 运算符 #算数运算符# x=10# y=3## print(x / y)   除# print(x // y)  除取整数## print(x % y) #取余 # print(y**3) 求y的三次方 #了解部分#字符串+,*#列表:+,*# l1=[1,2,3]# l2=[4,5]## print(l1+l2)# print(l1*3)  输出3个l1 #比较运算符# num1=3# num2=1 # print(num1 > num2)# print(num1 < num