---
title: "两个Python练习题"
author: "cool010"
date: "2013-08-17T19:03:03Z"
tags: []
categories: ["编程语言"]
slug: "python"
draft: false
lastmod: "2013-08-17T19:02:31Z"
id: 1926521
url: "https://cool010.iteye.com/blog/1926521"
---

1．假设校园电费是0.6元/千瓦时，输入这个月使用了多少千瓦时的点，算出你要交的电费
。假如你只有1元和1毛的硬币，请问各需要多少1元和1毛的硬币。
输入输出：
输入这个月使用的电量：11
电费：6.6
共需6张1元和6张1毛
 
```python
def main():
     pq = input('please input the power quantity:')
     price = 0.6
     amt = pq * price * 10
     i = int(amt) / 10
     j = int(amt) % 10 / 1
     print 'It will spend you %d yuan and %d jiao' % (i,j)
```

    
>>> main()
please input the power quantity:32
It will spend you 19 yuan and 1 jiao

2.假设已加密系统采用替换法进行加密，替换的规则如下：
明文      a       b       c       d       e       f       g       h       i       j       k       l       m       n       o       p       q       r       s       t       u       v       w       x       y       z
密文      q       w       e       r       t       y       u       i       o       p       a       s       d       f       g       h       j       k       l       z       x       c       v       b       n       m
设计一程序，输入一串明文，输出它对应的密文

1.  使用find
 
```python
def decode():
     strKey = 'abcdefghijklmnopqrstuvwxyz'
     strValue = 'qwertyuiopasdfghjklzxcvbnm'
     strIn = raw_input('please enter some words:')
     strOut = ''
     for i in range(len(strIn)):
          strOut += strValue[strkey.find(strin[i])]
     print 'decode result is :%s' % (strOut)
```


>>> decode()
please enter some words:abcefeg
decode result is :qwetytu

2.使用字典

 
```python
def docode():
     strKey = 'abcdefghijklmnopqrstuvwxyz'
     strValue = 'qwertyuiopasdfghjklzxcvbnm'
     strIn = raw_input('please enter some words:')
     dictDecode = {}
     strOut = ''
     for i in range(len(strKey)):
          dictDecode[strkey[i]] = strValue[i]
     for i in range(len(strIn)):
          strOut += dictDecode[strin[i]]
     print 'decode result is :%s' % (strOut)
```


>>> decode()
please enter some words:helloworld
decode result is :itssgvgksr