2つのPython練習問題


1.仮に学校の電気料金が0.6元/キロワット時だとして、今月何キロワット時を使ったかを入力して、あなたが払う電気料金を算出します
.1元と1毛の硬貨しかない場合は、それぞれ1元と1毛の硬貨が必要ですか.
入出力:
今月使用した電力量を入力:11
電気料金:6.6
全部で1元6枚と1毛6枚必要です
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 cd e f g h i j kl mn 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 kl 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