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

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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值