Udemy- python零基础入门100天训练营
1、Caesercode简介
以26个英文字母为基础,将英文转换为caeser加密的模式,比如hello, 将h按照字母表顺序向后挪几个(可设置挪动的个数),利用encode和decode来加密和解密文字。
2、练习1:做一个有三个因素的简单计算function(刷墙需要多少桶油漆?)
#day 8 exercise 1 how many cans of paints needed?
def paint_calc(height, width, cover):
print(f"you'll need {(height * width)/coverage} of cans.")
test_h = int(input("Height of wall: "))
test_w = int(input("Width of wall: "))
coverage = 5
paint_calc(height=test_h, width=test_w, cover=coverage)
#exercise2 check prime number
def prime_checker(number):
is_prime = True
for i in range(2,number):
if i % number == 0:
is_prime = False
if is_prime:
print('It is a prime number')
else:
print('It is not a prime number')
n = int(input("Check this number: "))
#call this defined funciton
prime_checker(number=n)
3、练习2:找出质数的function
#exercise2 check prime number
def prime_checker(number):
is_prime = True
for i in range(2,number):
if i % number == 0:
is_prime = False
if is_prime:
print('It is a prime number')
else:
print('It is not a prime number')
n = int(input("Check this number: "))
#call this defined funciton
prime_checker(number=n)
4、caeser 的encode部分
#cipher text encode exercise
alphabet = ['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','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']
direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))
#TODO-1: Create a function called 'encrypt' that takes the 'text' and 'shift' as inputs.
def encrypt(plain_text, shift_amount):
#TODO-2: Inside the 'encrypt' function, shift each letter of the 'text' forwards in the alphabet by the shift amount and print the encrypted text.
cipher_text = ""
for letter in plain_text:
position = alphabet.index(letter)
#index means the position
new_position = position + shift_amount
new_letter = alphabet[new_position]
cipher_text += new_letter
print(f'The encoded text is {cipher_text}')
#TODO-3: Call the encrypt function and pass in the user inputs. You should be able to test the code and encrypt a message.
encrypt(plain_text = text, shift_amount = shift)
5、caeser 的decode部分
#cipher text decode exercise
def decrypt(cipher_text, shift_amount):
plain_text = ""
for letter in cipher_text:
position = alphabet.index(letter)
new_position = position - shift_amount
plain_text +=alphabet[new_position]
print(f"The decoded text is {plain_text}")
#TODO-2: Inside the 'decrypt' function, shift each letter of the 'text' *backwards* in the alphabet by the shift amount and print the decrypted text.
#TODO-3: Check if the user wanted to encrypt or decrypt the message by checking the 'direction' variable. Then call the correct function based on that 'drection' variable. You should be able to test the code to encrypt *AND* decrypt a message.
if direction == 'encode':
encrypt(plain_text=text, shift_amount=shift)
elif direction == 'decode':
decrypt(cipher_text = text, shift_amount = shift)
6、合并encode和decode部分
#combine version of caeser code
def caesar(start_text, shift_amount, cipher_direction):
end_text = ""
if cipher_direction == 'decode':
shift_amount *= -1
for letter in start_text:
position = alphabet.index(letter)
new_position = position + shift_amount
end_text = alphabet[new_position]
print(f'The {cipher_direction}d text is {end_text}')
caeser(start_text=text, shift_amount=shift,cipher_direction=direction)
7、完整caesercode 代码
#final caeser code
logo = """
,adPPYba, ,adPPYYba, ,adPPYba, ,adPPYba, ,adPPYYba, 8b,dPPYba,
a8" "" "" `Y8 a8P_____88 I8[ "" "" `Y8 88P' "Y8
8b ,adPPPPP88 8PP""""""" `"Y8ba, ,adPPPPP88 88
"8a, ,aa 88, ,88 "8b, ,aa aa ]8I 88, ,88 88
`"Ybbd8"' `"8bbdP"Y8 `"Ybbd8"' `"YbbdP"' `"8bbdP"Y8 88
88 88
"" 88
88
,adPPYba, 88 8b,dPPYba, 88,dPPYba, ,adPPYba, 8b,dPPYba,
a8" "" 88 88P' "8a 88P' "8a a8P_____88 88P' "Y8
8b 88 88 d8 88 88 8PP""""""" 88
"8a, ,aa 88 88b, ,a8" 88 88 "8b, ,aa 88
`"Ybbd8"' 88 88`YbbdP"' 88 88 `"Ybbd8"' 88
88
88
"""
alphabet = ['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', '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']
def caesar(start_text, shift_amount, cipher_direction):
end_text = ""
if cipher_direction == "decode":
shift_amount *= -1
for char in start_text:
if char in alphabet:
position = alphabet.index(char)
new_position = position + shift_amount
end_text += alphabet[new_position]
else:
end_text+=char
#TODO-3: What happens if the user enters a number/symbol/space?
print(f"Here's the {cipher_direction}d result: {end_text}")
#TODO-1: Import and print the logo from art.py when the program starts.
from art import logo
#TODO-4: Can you figure out a way to ask the user if they want to restart the cipher program?
should_continue = True
while should_continue:
direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))
#TODO-2: What if the user enters a shift that is greater than the number of letters in the alphabet?
shift = shift%26
#what if shift=5?
caesar(start_text=text, shift_amount=shift, cipher_direction=direction)
result = input("Type 'yes' to continue, otherwise type 'no'.\n")
if result == 'no':
should_continue = False
print('goodbye')
#2022.11.23FJ没有好好学习
2022.11.23 晚上21:49于家中桌子上,其实已经开始学day9了,但是今晚要看球所以不学了!!