(Day 08) Task : Function Parameters & Caesar Cipher :-

Table of contents
- Goal of the Lesson :
- What is a Function?
- What are Parameters and Arguments?
- Function with Inputs (Parameters) :-
- Task : Life in Weeks :-
- Positional vs Keyword Arguments :-
- Task :Love Calculator :-
- What is Caesar Cipher?
- Caesar Cipher Part 1 ( Encryption ) :-
- What Does "Encoding" Mean?
- Basic Idea of Caesar Cipher
- Example:
- Caesar Cipher Part 2 ( Decryption ) :-
- What Is Decryption?
- Basic Idea of Caesar Cipher Decryption :
- Example
- Caesar Cipher Part 3 ( Reorganising Our Code ) :-

Goal of the Lesson :
Learn how to use function parameters and arguments.
Understand how to pass values into a function.
Build a Caesar Cipher tool that can encode and decode a message by shifting letters.
What is a Function?
A function is a reusable block of code that performs a specific task. Instead of repeating the same code again and again, we define a function once and call it whenever needed.
Syntax of a Basic Function :
def function_name():
# Code block
This defines a function with no inputs. But what if we want it to behave differently depending on the data we give it?
That’s where function inputs (also called parameters or arguments) come in.
What are Parameters and Arguments?
Parameters :
These are like empty boxes inside the function — they are used to receive values when the function is called.
You define them when you create the function.
def greet(name): # 'name' is a parameter
print(f"Hello, {name}!")
Arguments :
These are the actual values you send into the function when you call it.
greet("Alice") # "Alice" is the argument passed to the 'name' parameter
Function with Inputs (Parameters) :-
Why Inputs Are Needed :
Inputs allow us to customize the behavior of a function. Instead of hardcoding values, we can pass them in.
Syntax:
def function_name(parameter1, parameter2):
# Code block using parameter1 and parameter2
Example:
def greet(name):
print(f"Hello, {name}!")
Function Call:
greet("Alice") # Output: Hello, Alice!
greet("Bob") # Output: Hello, Bob!
→ name
is the parameter
→ "Alice"
is the argument passed to the function
Using Multiple Inputs :
You can pass more than one input:
def greet_with(name, location):
print(f"Hello {name} from {location}!")
greet_with("Alice", "India") # Hello Alice from India!
Task : Life in Weeks :-
I was reading this article by Tim Urban - Your Life in Weeks and realised just how little time we actually have.
Create a function called life_in_weeks()
using maths and f-Strings that tells us how many weeks we have left, if we live until 90 years old.
It will take your current age as the input and output a message with our time left in this format:
You have x weeks left.
Where x is replaced with the actual calculated number of weeks the input age has left until age 90.
Warning : The function must be called life_in_weeks
for the tests to pass. Also the output must have the same punctuation and spelling as the example. Including the full stop!
Example Input
56
Example Output
You have 1768 weeks left.
How to test your code and see your output?
Udemy coding exercises do not have a console, so you cannot use input()
. You will need to call your function with hard-coded values like so:
def life_in_weeks(age):
# your code here# Call your function with hard coded valueslife_in_weeks(12)
Code :
def life_in_weeks(age):
years_left = 90 - age
weeks_left = years_left * 52
print(f"You have {weeks_left} weeks left.")
# Call the function with a hard-coded value
life_in_weeks(56)
Positional vs Keyword Arguments :-
Positional Arguments :
Values are passed in the same order as the parameters.
greet_with("Alice", "India") # name = Alice, location = India
Keyword Arguments :
You specify the parameter name when passing the argument.
greet_with(location="India", name="Alice") # Works the same
Task :Love Calculator :-
This is a difficult challenge!
You are going to write a function called calculate_love_score()
that tests the compatibility between two names. To work out the love score between two people:
1. Take both people's names and check for the number of times the letters in the word TRUE occurs.
2. Then check for the number of times the letters in the word LOVE occurs.
3. Then combine these numbers to make a 2 digit number and print it out.
Example :
name1 = "Angela Yu" name2 = "Jack Bauer"
T occurs 0 times
R occurs 1 time
U occurs 2 times
E occurs 2 times
Total = 5
L occurs 1 time
O occurs 0 times
V occurs 0 times
E occurs 2 times
Total = 3
Love Score = 53
Example Input
calculate_love_score("Kanye West", "Kim Kardashian")
Example Output
42
How to test your code and see your output?
Udemy coding exercises do not have a console, so you cannot use the input()
function. You will need to call your function with hard-coded values like so:
def calculate_love_score(name1, name2)
# your code here.# Call your function with hard coded value.calculate_love_score("Kanye West", "Kim Kardashian")
Code :
def calculate_love_score(name1, name2):
# Combine both names and convert to lowercase
combined_names = (name1 + name2).lower()
# Define the words to check
true_count = 0
love_count = 0
# Count letters in 'TRUE'
for letter in "true":
true_count += combined_names.count(letter)
# Count letters in 'LOVE'
for letter in "love":
love_count += combined_names.count(letter)
# Combine both counts into a two-digit number
love_score = int(str(true_count) + str(love_count))
# Print the final score
print(f"Your love score is {love_score}")
# Example call
calculate_love_score("Kanye West", "Kim Kardashian")
What is Caesar Cipher?
The Caesar Cipher is one of the simplest and oldest encryption techniques. It was used by Julius Caesar to send confidential messages to his army. It is a type of substitution cipher, where each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet.
How It Works :
Choose a shift number (e.g., 3).
Replace each letter in the message with the letter that is shifted forward by that number.
If the shift goes beyond 'z', it wraps around to the start of the alphabet.
Example :
Shift each letter 3 steps forward:
a → d
p → s
p → s
l → o
e → h
So, "apple"
becomes: "dssoh"
Caesar Cipher Part 1 ( Encryption ) :-
What Does "Encoding" Mean?
Encoding means converting plain text into secret (cipher) text using a shift number.
Basic Idea of Caesar Cipher
Each letter is replaced by another letter that is a few steps forward in the alphabet.
If you reach the end of the alphabet, you wrap around to the beginning.
Example:
Let's encode the word "encryption"
using a shift of 3
.
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
Plain Text: encryption
Shift: 3
Encrypted Text: hqfubswlrq
Explanation:
e → h
n → q
c → f
r → u
y → b
p → s
t → w
i → l
o → r
n → q
we are going to build an encryption and decryption program using the Caesar cipher.
Demo
https://appbrewery.github.io/python-day8-demo/
TODO-1 :
Create a function called encrypt() that takes original_text and shift_amount as 2 inputs.
TODO-2 :
Inside the 'encrypt' function, shift each letter of the original_text forwards in the alphabet by the shift_amount and print the encrypted text.
You can use the built-in Python index() function to find out the position of an item in a list. e.g.
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']
alphabets.index("a") #1
Example: If we have following values:
plain_text = "encryption"
shift_amount = 3
The final encrypted output should be:
Here is the encoded result: ifmmp
Where each of the letters of 'hello' is shifted up by 1.
TODO-3 :
What happens if you try to shift the letter 'z' forwards by 9? Can you fix the code?
shifted_position %= len(alphabet)
TODO-4 :
Call the encrypt() function and pass in the user inputs. You should be able to test the code and encrypt a message.
encrypt(original_text= text , shift_amount= shift)
Caesar Cipher Part 2 ( Decryption ) :-
What Is Decryption?
Decryption is the process of turning the encoded (secret) message back into normal (plain) text using the same shift that was used during encryption.
In simple words, it’s undoing the shift applied during encryption.
Basic Idea of Caesar Cipher Decryption :
Every letter in the message is shifted backward in the alphabet by the given shift number.
If a shift goes before
'a'
, it wraps around to'z'
.
Example
Let’s decrypt "ifmmp"
with a shift of 1
.
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
Shift each letter backward by 3:
i → h
f → e
m → l
m → l
p → o
So, "ifmmp"
becomes:
➡️ "hello"
TODO-1 :
Create a function called decrypt() that takes original_text and shift_amount as 2 inputs.
TODO-2 :
Inside the decrypt() function, shift each letter of the original_text forwards in the alphabet backwards by the shift_amount and print the decrypted text.
def decrpyt(original_text, shift_amount):
cipher_text = ""
for letter in original_text:
shifted_position = alphabet.index(letter) - shift_amount
shifted_position %= len(alphabet)
cipher_text += alphabet[shifted_position]
print(f"Here is the decoded result: {cipher_text}")
decrpyt(original_text=text, shift_amount=shift)
Now let’s Make a Function of both functions and make a single function using them :
Combine the encrypt() and decrypt() functions into a single function called caesar().
Use the value of the user chosen direction variable to determine which functionality to use.
call the caesar function instead of encrypt/decrypt and pass in all three variables direction/text/shift.
def caesar(original_text , shift_amount , encode_or_decode):
output_text = ""
for letter in original_text:
if encode_or_decode == "decode":
shift_amount *= -1
shifted_position = alphabet.index(letter) + shift_amount
shifted_position %= len(alphabet)
output_text += alphabet[shifted_position]
print(f"Here is the decoded result: {output_text}")
decrpyt(original_text=text, shift_amount=shift , encode_or_decode = direction )
Caesar Cipher Part 3 ( Reorganising Our Code ) :-
TODO-1 :
Import the logo from art.py when the program starts.
Print the logo using
art.logo
TODO-2 :
What happens if the user enters a number/symbol/space that's not in the List alphabet?
Can you fix the code to keep the number/symbol/space when the text is encoded/decoded?
original_text = "meet me at 3!"
cipher_text = "XXXX XX XX 3!"
TODO-3 :
Can you figure out a way to restart the cipher program?
Example :
Type 'yes' if you want to go again. Otherwise, type 'no'.
If they type 'yes' then ask them for the direction/text/shift again and call the caesar() function again.
Code :
Subscribe to my newsletter
Read articles from Aditya Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Aditya Sharma
Aditya Sharma
DevOps Enthusiast | Python | Chef | Docker | GitHub | Linux | Shell Scripting | CI/CD & Cloud Learner | AWS