100 Days of Code: Day 8
Oluwatoyin Ariyo
Posted on January 21, 2023
Day 8 of Angela Yu's Python bootcamp was about functions with inputs are used in Python and also what the difference is between parameters and arguments (parameters are the name of the data being passed in and arguments are the value of the data).
Our goal of the day was to build a caesar cipher that asks the user to enter a message and the program shifts the letters by a number that the user inputs. Here is the Python code:
# TODO-1: Import and print the logo from art.py when the program starts.
from art import logo
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']
def caesar(start_text, shift_amount, cipher_direction):
end_text = ""
if cipher_direction == "decode":
shift_amount *= -1
for letter in start_text:
# TODO-3: What happens if the user enters a number/symbol/space? Can you fix the code to keep the number/symbol/space when the text is encoded/decoded?
if letter in alphabet:
position = alphabet.index(letter)
# TODO-2: What if the user enters a shift that is greater than the number of letters in the alphabet?
new_position = (position + shift_amount) % 26
end_text += alphabet[new_position]
else:
end_text += letter
print(f"The {cipher_direction}d text is {end_text}")
print(logo)
#TODO-4: Can you figure out a way to ask the user if they want to restart the cipher program?
should_end = False
while not should_end:
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"))
caesar(start_text=text, shift_amount=shift, cipher_direction=direction)
restart = input("Type 'yes' if you want to go again. Otherwise type 'no'.\n")
if restart == "no":
should_end = True
print("Goodbye")
I love doing these projects as I feel more confident in Python and programming. I also did the same project in C#. Here is the code for that:
string[] 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" };
void caesar(string start_text, int shift_amount, string cipher_direction)
{
string end_text = "";
if (cipher_direction == "decode")
{
shift_amount *= 1;
}
for (int i = 0; i < start_text.Length; i++)
{
char letter = start_text[i];
if (alphabet.Contains(letter.ToString()))
{
int position = Array.IndexOf(alphabet, letter.ToString());
int new_position = (position + shift_amount) % 26;
end_text += alphabet[new_position];
}
else
{
end_text += letter;
}
}
Console.WriteLine("The {0}d text is {1}", cipher_direction, end_text);
}
bool should_end = false;
while (!should_end)
{
Console.WriteLine("Type 'encode' to encrypt, type 'decode' to decrypt");
string direction = Console.ReadLine().ToLower();
Console.WriteLine("Type your message: ");
string text = Console.ReadLine().ToLower();
Console.WriteLine("Type the shift number:");
int shift = Convert.ToInt32(Console.ReadLine());
caesar(start_text: text, shift_amount: shift, cipher_direction: direction);
Console.WriteLine("Type 'yes' if you want to go again otherwise type 'no'.");
string restart = Console.ReadLine().ToLower();
if (restart == "no")
{
should_end= true;
Console.WriteLine("Goodbye");
}
}
It was a bit difficult to convert the Python code to C# mainly because the syntax of for loops in C# are more complex than Python's for loop syntax. There is also a lot more converting variables into different data types like converting letter to string.
I will post about Day 9 next week.
Posted on January 21, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.