两个Python练习题

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值