100 Days of Code: The Complete Python Pro Bootcamp for 2022 - Day 8 (Caesar Cipher)
Exercise 1 - Paint area calculator
#Write your code below this line ๐
importmathdefpaint_calc(height,width,cover):num_of_cans=(height*width)/coverround_up_cans=math.ceil(num_of_cans)print(f"You'll need {round_up_cans} cans of paint.")#Write your code above this line ๐
#Define a function called paint_calc() so that the code below works.
#๐จ Don't change the code below ๐
test_h=int(input("Height of wall: "))test_w=int(input("Width of wall: "))coverage=5paint_calc(height=test_h,width=test_w,cover=coverage)
Exercise 2 - Prime number checker
#Write your code below this line ๐
defprime_checker(number):is_prime=Trueforiinrange(2,number):ifnumber%i==0:is_prime=Falseifis_prime: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)
Project Caesar Cipher Part 1 - Encrypt
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','0','1','2','3','4','5']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.
defencrypt(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.
#e.g.
#plain_text = "hello"
#shift = 5
#cipher_text = "mjqqt"
#print output: "The encoded text is mjqqt"
cipher_text=""forletterinplain_text:position=alphabet.index(letter)new_position=position+shift_amountnew_letter=alphabet[new_position]cipher_text+=new_letterprint(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)#HINT: How do you get the index of an item in a list:
#https://stackoverflow.com/questions/176918/finding-the-index-of-an-item-in-a-list
#๐Bug alert: What happens if you try to encode the word 'civilization'?๐
# Answer = IndexError: list index out of range - The shift cant move the postion when the letter z is used. Probably the same for letters at the end of the alphabet depending on the shift ampunt. Work around is alpha numeric, add numbers 0-10 into the list.
Anotheroptionistocopythelistalphabeta-zandpastesothatthereare2xa-zinthealplhabetlist.
Caesar Cipher Part 2 - Decrypt
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"))defencrypt(plain_text,shift_amount):cipher_text=""forletterinplain_text:position=alphabet.index(letter)new_position=position+shift_amountcipher_text+=alphabet[new_position]print(f"The encoded text is {cipher_text}")#TODO-1: Create a different function called 'decrypt' that takes the 'text' and 'shift' as inputs.
defdecrypt(cipher_text,shift_amount):plain_text=""forletterincipher_text:position=alphabet.index(letter)old_position=position-shift_amountplain_text+=alphabet[old_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.
#e.g.
#cipher_text = "mjqqt"
#shift = 5
#plain_text = "hello"
#print output: "The decoded text is hello"
#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.
ifdirection=="encode":encrypt(plain_text=text,shift_amount=shift)else:decrypt(cipher_text=text,shift_amount=shift)
Caesar Cipher Part 3 - Reorganising the code
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: Combine the encrypt() and decrypt() functions into a single function called caesar().
defcaesar(start_text,shift_amount,cipher_direction):end_text=""ifcipher_direction=="decode":shift_amount*=-1forletterinstart_text:position=alphabet.index(letter)new_position=position+shift_amountend_text+=alphabet[new_position]print(f"Here's the {direction}d result: {end_text}")caesar(start_text=text,shift_amount=shift,cipher_direction=direction)#TODO-2: Call the caesar() function, passing over the 'text', 'shift' and 'direction' values.
Caesar Cipher Part 4 - User Exp Improv and 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','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']defcaesar(start_text,shift_amount,cipher_direction):end_text=""ifcipher_direction=="decode":shift_amount*=-1forcharinstart_text:ifcharinalphabet:position=alphabet.index(char)new_position=position+shift_amountend_text+=alphabet[new_position]else:end_text+=char#TODO-3: What happens if the user enters a
number/symbol/space?#Can you fix the code to keep the number/symbol/space when
thetextisencoded/decoded?#e.g. start_text = "meet me at 3"
#end_text = "โขโขโขโข โขโข โขโข 3"
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.
fromartimportlogoprint(logo)#TODO-4: Can you figure out a way to ask the user if they want to restart the cipher program?
#e.g. 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?
#Hint: Try creating a while loop that continues to execute the program if the user types 'yes'.
should_continue=Truewhileshould_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
thenumberoflettersinthealphabet?#Try running the program and entering a shift number of 45.
#Add some code so that the program continues to work even if
theuserentersashiftnumbergreaterthan26.#Hint: Think about how you can use the modulus (%).
shift=shift%26caesar(start_text=text,shift_amount=shift,cipher_direction=direction)result=input("Type 'yes' if you want to go again. Otherwise type 'no'. ")ifresult=="no":should_continue=Falseprint("Goodbye.")
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)