ITeye博客

两个Python练习题

cool010 · 2013-08-17 19:03 · 编程语言 python · 编程语言 · Markdown

1.假设校园电费是0.6元/千瓦时,输入这个月使用了多少千瓦时的点,算出你要交的电费 。假如你只有1元和1毛的硬币,请问各需要多少1元和1毛的硬币。 输入输出: 输入这个月使用的电量:11 电费:6.6 共需6张1元和6张1毛

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
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.使用字典

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。