Introduction
Welcome to Day 8 of the 100 Days of Code challenge! Today’s focus was on understanding functions, particularly defining and calling them. Let’s dive into what I’ve learned and implemented.
Caesar Cipher Project:
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
"""
print(logo)
program = True
while program is True:
if input("Do you want to decode or encode a message? (yes/no): ") == "yes":
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']
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"))
def ceasar(text,shift,direction):
plain_text = ""
if direction == "decode":
shift *= -1
for letter in text:
if letter in alphabet:
position = alphabet.index(letter)
new_position = (position + shift) % 26
plain_text += alphabet[new_position]
else:
plain_text += letter
print(f"The {direction}d text is {plain_text}")
ceasar(text,shift,direction)
else:
print("Goodbye")
input("Press enter to exit")
program = False
PythonDescription:
The Caesar Cipher project involves creating a program to encrypt and decrypt messages using a simple substitution cipher. Here’s a breakdown of the key elements:
- Function Definition: A function named
Ceasar
is defined to handle the encryption or decryption process based on the user’s choice. - User Interaction: Users are prompted to choose whether they want to encode or decode a message and provide the necessary inputs such as the text, shift number, and direction.
- Encryption/Decryption Logic: The core logic of the Caesar Cipher lies in shifting each letter of the input text by a fixed number of positions (the shift value). This is achieved by iterating through the text, shifting each letter accordingly while preserving non-alphabetic characters.
- Output: The result of the encryption or decryption process is displayed to the user.
Prime Number Function:
def prime_number(number):
"""
This function checks if a given number is prime or not.
Parameters:
- number: An integer to be checked for primality.
Returns:
- None. Prints whether the number is prime or not.
"""
is_prime = True
for num in range(2, number):
if number % num == 0:
is_prime = False
if is_prime:
print(f"{number} is a prime number")
else:
print(f"{number} is not a prime number")
# Example usage:
number = int(input("Enter a number: "))
prime_number(number)
PythonDescription:
The prime number
function takes an integer input and determines whether it is a prime number or not. It iterates through numbers starting from 2 up to the input number minus 1. If the input number is divisible by any number other than 1 and itself, it is not prime. Otherwise, it is prime. The function then prints the result.
This function can be useful in various scenarios, such as mathematical computations, number theory, or algorithmic problem-solving.
Feel free to integrate this function into your projects or use it for educational purposes!
Learning Outcome:
Today, I gained a deeper understanding of how to define and call functions in Python. Additionally, I enhanced my knowledge of string manipulation and basic encryption techniques through the implementation of the Caesar Cipher project.
Stay tuned for more updates on my coding journey!
This is part of the 100 Days of Code challenge.