Python day 8_Beginner Function parameters & Caesar Cipher
Functions with input (review)
# Review:
# Create a function called greet().
# Write 3 print statements inside the function.
# Call the greet() function and run your code.
def greet():
print("Hello")
print("This is a review for functions")
print("Good Bye")
greet()
Adding variations in our functions (Can be modified)
You can add input into the parenthesis such as
def my_function(something):
#Do this with something
def greet_with_name(name):
print(f"Hello {name}")
print(f"How do you do {name}?")
greet_with_name("Python")
Argument is the actual piece of data that’s going to be passed over in the function when its being called (Python)
Parameter is the name of that data and we use the parameter inside of the function to do refer to it and to do things with it. (something)
Positional vs keyword arguments
#funtions with more than 1 input
def greet_with(name, location):
print(f"Hello {name} from {location}")
print(f"{name} hows the weather at {location}")
greet_with("Python", "Space") -> This is positional argument as the output can be changed depending on the position of the argument. (They values can be switched)
Keyword arguments
def my_function(a, b, c):
#Do this with a
#Then do this with b
#Finally do this with c
my_function (a=1, b=2, c=3) -> Even if you change it like so my_function(c=3, a=1, b=2)
Doesn’t matter if the orders are different the values are the same therefore it will still print the same
def greet_key(name, location):
print(f"Hello {name}")
print(f"How is the weather at {location}")
greet_key(name = "Python", location = "Space")
Paint Area Calculator Coding exercise
# Write your code below this line 👇
def paint_calc(height, width, cover):
import math
number_of_cans = math.ceil((test_h * test_w) / coverage)
print(f"You'll need {number_of_cans} cans of paint.")
# Write your code above this line 👆
# Define a function called paint_calc() so the code below works.
# 🚨 Don't change the code below 👇
test_h = int(input()) # Height of wall (m)
test_w = int(input()) # Width of wall (m)
coverage = 5
paint_calc(height=test_h, width=test_w, cover=coverage)
Prime Number Checker coding Exercise
# Write your code below this line 👇
def prime_checker(number):
if n > 1:
for i in range(2, int(n/2)+1):
if (n % i) == 0:
print("It's not a prime number.")
break
else:
print("It's a prime number.")
else:
print("It's not a prime number.")
# Write your code above this line 👆
#Do NOT change any of the code below👇
n = int(input()) # Check this number
prime_checker(number=n)
Alternative code
is_prime = True
for I in range(2, number):
if number % I == 0:
is_prime = False
if is_prime:
print("It's a prime number.")
else:
print("It's not a prime number.")
Caesar Cipher Part 1 – Encryption
Shift the Alphabets to the right by X number of time.
def encrypt(text, shift):
encrypted_text = ""
for char in text:
if char in alphabet:
# Find the index of the current character in the alphabet
index = alphabet.index(char)
# Shift the index by the specified shift amount
new_index = (index + shift) % 26
# Append the encrypted character to the result string
encrypted_text += alphabet[new_index]
else:
# If the character is not in the alphabet (e.g., space or punctuation), leave it unchanged
encrypted_text += char
print(f"The encoded text is {encrypted_text}")
encrypt(text, shift)
Caesar Cipher Part 2 – Decryption
def decrypt(text, shift):
decrypted_text = ""
for char in text:
if char in alphabet:
index = alphabet.index(char)
new_index = (index - shift) % 26
decrypted_text += alphabet[new_index]
else:
decrypted_text += char
print(f"The decoded text is {decrypted_text}")
Caesar Cipher Part 3 – Reorganizing the code
def caesar(text, shift, direction):
end_text = ""
if direction == "decode":
shift *= -1
for char in text:
if char in alphabet:
index = alphabet.index(char)
new_index = (index + shift) % 26
end_text += alphabet[new_index]
print(f"Here's the {direction}d result: {end_text}")
caesar(text, shift, direction)
Caesar Cipher Part 4 – User Experience improvements & Final touches
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 caesar(text, shift, direction):
end_text = ""
if direction == "decode":
shift *= -1
for char in text:
#When users use number/symbol/space add the else statement to ignore numbers and symbols
if char in alphabet:
index = alphabet.index(char)
new_index = (index + shift) % 26
end_text += alphabet[new_index]
else:
end_text += char
print(f"Here's the {direction}d result: {end_text}")
caesar(text, shift, direction)
#Loop the function to continue the Cipher program
while True:
restart = input("Do you want to continue? Type 'yes' or 'no':\n").lower()
if restart == "no":
break
if restart == "yes":
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(text, shift, direction)
print("Thank you for using Caesar Cipher")
'Coding : Python' 카테고리의 다른 글
Python_Day 9 (0) | 2023.12.11 |
---|---|
Python_Day 7 (0) | 2023.12.02 |
Python_Day 6 (0) | 2023.11.30 |
Python_Day 5 (1) | 2023.11.23 |
Python_Day 4 (0) | 2023.11.17 |