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

来源:互联网 发布:嵌入式linux内核开发 编辑:程序博客网 时间:2024/06/05 03:55
示例:
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.

原创粉丝点击