Python笔记(7)----相关示例

示例:
sentence = raw_input("sentence; ")
screen_width = 80
text_width = len(sentence)
box_width = text_width + 4
left_margin = (screen_width - box_width) // 2


print
print ' '* left_margin + '+'+'-'* (box_width-4) + '+'
print ' '* left_margin + '|'+' '* text_width    + '|'
print ' '* left_margin + '|'+    sentence       + '|'
print ' '* left_margin + '|'+' '* text_width    + '|'
print ' '* left_margin + '+'+'-'* (box_width-4) + '+'
print


结果:
sentence; fighting!


                                 +---------+
                                 |              |
                                 |fighting!|
                                 |              |
                                 +---------+


示例:
database = [
    ['luo','123'],
    ['ren','234'],
    ['huo','345'],
    ['zhu','456'],
]
username = raw_input('user name; ')
pin =raw_input('pin code; ')
if[username,pin] in database:
    print 'Access granted'
else :
    print 'Access ungranted'


结果:
user name; luo
pin code; 123
Access granted
结果:
user name; ren
pin code; 123
Access ungranted


示例:
width = input('please enter width: ')
price_width = 10
item_width = width - price_width
header_format = '%-*s%*s'
format = '%-*s%*.2f'
print '='* width
print header_format % (item_width,'item',price_width,'price')
print '-'* width
print format % (item_width,'apples',price_width,0.4)
print format % (item_width,'pears',price_width,0.5)
print format % (item_width,'cantaloupes',price_width,1.92)
print format % (item_width,'dried apricots',price_width,8)
print format % (item_width,'prunes',price_width,12)
print '=' * width


结果:
please enter width: 35
===================================
item                          price
-----------------------------------
apples                         0.40
pears                          0.50
cantaloupes                    1.92
dried apricots                 8.00
prunes                        12.00
===================================


示例:
people = {
    'luo':{
        'phone':'1234',
        'addr':'foo dive 23'
    },
    'ren':{
        'phone':'2345',
        'addr':'bar street 42'
    },
    'huo':{
        'phone':'3456',
        'addr':'baz avenue 90'
    }


}
labels = {
    'phone':'phone number',
    'addr':'address'
}
name = raw_input('name: ')
request = raw_input('phone number(p) or address(a)?')
if request == 'p':key = 'phone'
if request == 'a':key = 'addr'
if name in people: print "%s's %s is %s." % (name, labels[key],people[name][key])


结果:
name: luo
phone number(p) or address(a)?p
luo's phone number is 1234.


示例:
people = {
    'luo':{
        'phone':'1234',
        'addr':'foo dive 23'
    },
    'ren':{
        'phone':'2345',
        'addr':'bar street 42'
    },
    'huo':{
        'phone':'3456',
        'addr':'baz avenue 90'
    }


}
labels = {
    'phone':'phone number',
    'addr':'address'
}
name = raw_input('name: ')
request = raw_input('phone number(p) or address(a)?')
key = request
if request == 'p':key = 'phone'
if request =='a':key ='addr'


person = people.get(name,{})
label = labels.get(key,key)
result = person.get(key,'not available')


print "%s's %s is %s."% (name,label,result)


结果:
name: ren
phone number(p) or address(a)?a
ren's address is bar street 42.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值