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.
sentence = raw_input("sentence; ")
screen_width = 80
text_width = len(sentence)
box_width = text_width + 4
left_margin = (screen_width - box_width) // 2
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) + '+'
结果:
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.
阅读全文
0 0
- Python笔记(7)----相关示例
- python相关笔记
- python入门相关笔记
- python模块 相关笔记
- Python __future__模块常见示例相关解读
- python学习笔记-(11)python中的文件相关
- Python 之 Scrapy笔记(2)- 完整示例
- python学习笔记(十)错误处理、Matplotlib绘图示例
- 《Python进行自然语言处理》代码笔记(一):第一章示例
- Python学习笔记(5)--类相关语法
- python -DBSAN-笔记(及相关问题)11-02-2017
- Python亲和性分析示例-学习笔记
- Python-分类问题示例-OneR-学习笔记
- php扩展开发笔记(5)一些数组操作相关的宏和简单示例
- C调用Python函数相关代码示例剖析
- python 内建函数详解及相关示例
- C调用Python函数相关代码示例剖析
- javascript中关于DOM的代码示例及相关笔记
- R语言中qplot()的用法
- day01笔记(计算机基础知识)
- VS Code 使用
- uml视频系列(二)——uml的概述
- 1170: 多项式相加
- Python笔记(7)----相关示例
- json 返回 不知道为什么 还要在放在一个 array里面
- Python 闭包与装饰器
- 邮件服务
- HTTPS那些事(一)HTTPS原理
- IPython 进入 pylab 模式
- 设计模式-设计原则
- Java compiler level does not match the version of the installed Java project facet.
- java常用的log系统