您的位置:首页 > 编程语言 > Python开发

python基础学习02(if...else...elif)

2016-04-27 23:55 741 查看
if...else...elif

格式化打印

[root@node1 day1]# vi three.py

"""

#!/usr/bin/env python

name = raw_input('what is your name?:')

print 'Hello, %s where are you from?' % name

[root@node1 day1]# python three.py

what is your name?:wolf

Hello, wolf where are you from?

格式化输出

#!/usr/bin/env python

name = raw_input('what is your name?:')

age = raw_input('how old are you?:')

print 'Hello, %s ,you are %s years old, where are you from?' % name

[root@node1 day1]# python three.py

what is your name?:wolf

how old are you?:22

Hello, wolf ,you are 22 years old, where are you from?

多行全部打印

#!/usr/bin/env python

name = raw_input('what is your name?:')

age = raw_input('how old are you?:')

age = raw_input('what is your job?')

msg = """

Information of %s as below:

Name : %s

Age : %s

Job : %s

""" %(name,name,age,job)

print msg

执行

[root@node1 day1]# python three.py

what is your name?:wolf

how old are you?:22

what is your job?it loser

Information of wolf as below:

Name : wolf

Age : 22

Job : it loser

加判断时

#!/usr/bin/env python

name = raw_input('what is your name?:')

#int将字符串转换为数字

age = int(raw_input('how old are you?:'))

job = raw_input('what is your job?')

msg = """

Information of %s as below:

Name : %s

Age : %s

Job : %s

""" %(name,name,age,job)

#type查看字符类型

print type(age)

if age >=30:

print "You are too old, you can only work for ..."

else:

# pass

print 'you are still very young!'

print msg

[root@node1 day1]# python three.py

what is your name?:wolf

how old are you?:49

what is your job?it

<type 'str'>

You are too old, you can only work for ...

Information of wolf as below:

Name : wolf

Age : 49

Job : it

加颜色

#!/usr/bin/env python

name = raw_input('what is your name?:')

age = raw_input('how old are you?:')

job = raw_input('what is your job?')

#有7中颜色 32是绿色

msg = """

Information of %s as below:

Name : \033[32;1m%s \033[0m;

Age : %s

Job : %s

""" %(name,name,age,job)

print type(age)

if int(age) >=30:

print "You are too old, you can only work for ..."

else:

# pass

print 'you are still very young!'

print msg

"three.py" 23L, 413C written

[root@node1 day1]# python three.py

what is your name?:wolf

how old are you?:22

what is your job?it

<type 'str'>

you are still very young!

Information of wolf as below:

Name : wolf ;

Age : 22

Job : it

加背景

#!/usr/bin/env python

name = raw_input('what is your name?:')

age = raw_input('how old are you?:')

job = raw_input('what is your job?')

#42是背景

msg = """

Information of %s as below:

Name : \033[42;1m%s \033[0m;

Age : %s

Job : %s

""" %(name,name,age,job)

print type(age)

if int(age) >=30:

print "You are too old, you can only work for ..."

else:

# pass

print 'you are still very young!'

print msg

"three.py" 23L, 413C written

[root@node1 day1]# python three.py

what is your name?:wolf

how old are you?:22

what is your job?it

<type 'str'>

you are still very young!

Information of wolf as below:

Name : wolf ;

Age : 22

Job : it

知识扩展:颜色和背景

多重判断

#!/usr/bin/env python

name = raw_input('what is your name?:')

age = raw_input('how old are you?:')

else:

job = raw_input('what is your job?')

msg = """

Information of %s as below:

Name : \033[42;1m%s \033[0m;

Age : %s

Job : %s

""" %(name,name,age,job)

print type(age)

if int(age) >=50:

print "You are too old, you can only work for ..."

elif int(age) >=30:

print "You are now in the middle age,so enjoy your life before getting too old...."

#最后一次判断按照逻辑不需要写条件了(逻辑很重要)

else:

# pass

print 'you are still very young!'

print msg

多重判断

#!/usr/bin/env python

name = raw_input('what is your name?:')

age = raw_input('how old are you?:')

job = raw_input('what is your job?')

msg = """

Information of %s as below:

Name : \033[42;1m%s \033[0m;

Age : %s

Job : %s

""" %(name,name,age,job)

print type(age)

if int(age) >=50:

print "You are too old, you can only work for ..."

elif int(age) >=30:

print "You are now in the middle age,so enjoy your life before getting too old...."

#elif int(age) >=20:

# print "You are young!!!"

else:

# pass

if int(age) >=20:

print 'you are still very young!'

else:

print 'you are not a adult!'

print msg
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: