Python 入門指南 V2.00 - 單元 17 範例及練習程式碼



encrypt04.py


# 使用 randint()
import random

# 定義 Encrypt 類別 
class Encrypt:
   def __init__(self):
      self.setcode()

   def setcode(self):
      # 取得 a 、 b 值
      a = random.randint(0, 9)
      print(a) # 印出 a
      b = random.randint(0, 9)
      print(b) # 印出 b
      
      # 利用公式建立密碼表
      self.code = ""
      c = "a"
      i = 0
      while i < 26:
         x = c
         y = ord(x) * a + b
         m = y % 26
         self.code += chr(m + 97)
         c = chr(ord(c) + 1)
         i += 1

   def getcode(self):
      return self.code

   # 編碼的方法
   def toEncode(self, str):
      pass

   # 解碼的方法 
   def toDecode(self, str):
      pass

# 測試部分
if __name__ == '__main__':
   e = Encrypt()
   print()
   print(e.getcode())
   print()
 
# 檔名: encrypt04.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

encrypt05.py


# 使用 randint()
import random

# 定義 Encrypt 類別 
class Encrypt:
   def __init__(self):
      self.setcode()

   def setcode(self):
      # 取得 a 、 b 值
      a = 0
      b = 0
      while a % 2 == 0: 
         a = random.randint(0, 9)
         b = random.randint(0, 9) 
      
      # 利用公式建立密碼表
      self.code = ""
      c = "a"
      i = 0
      while i < 26:
         x = c
         y = ord(x) * a + b
         m = y % 26
         self.code += chr(m + 97)
         c = chr(ord(c) + 1)
         i += 1

   def getcode(self):
      return self.code

   # 編碼的方法
   def toEncode(self, str):
      pass

   # 解碼的方法 
   def toDecode(self, str):
      pass

# 測試部分
if __name__ == '__main__':
   e = Encrypt()
   print()
   print(e.getcode())
   print()
 
# 檔名: encrypt05.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise1701.py


answer = "1234"

def ab_counter(answer, guess, ab):
   a = 0
   b = 0
   for i in guess:
      if i in answer:
         if guess.index(i) == answer.index(i):
            a += 1
         else:
            b += 1

   ab[0] = a
   ab[1] = b

if __name__ == "__main__":
   abcounter = [0, 0]
   times = 0
   while True:
      times += 1
      guess = input(": ")
      ab_counter(answer, guess, abcounter)
      if abcounter[0] == 4:
         print("Right!!")
         break
      else:
         print(str(abcounter[0]) + "A" + str(abcounter[1]) + "B")

   print("You guess " + str(times) + " times.")
 
# 檔名: exercise1701.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise1702.py


answer = "1234"

def ab_counter(answer, guess, ab):
   a = 0
   b = 0
   for i in guess:
      if i in answer:
         if guess.index(i) == answer.index(i):
            a += 1
         else:
            b += 1

   ab[0] = a
   ab[1] = b

def find_number(array):
   for i in array:
      if array.count(i) > 1:
         return True      

   return False

if __name__ == "__main__":
   abcounter = [0, 0]
   times = 0
   while True:
      times += 1
      guess = input(": ")
      if find_number(guess):
         print("Repeating digits!!")
         continue
      ab_counter(answer, guess, abcounter)
      if abcounter[0] == 4:
         print("Right!!")
         break
      else:
         print(str(abcounter[0]) + "A" + str(abcounter[1]) + "B")

   print("You guess " + str(times) + " times.")
 
# 檔名: exercise1702.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

the end

沒有留言: