Posts

Showing posts from December, 2021

Caesar cipher - improved version (final)

Image
This is an advanced version of the Caesar Cipher which has improved in case of graphics, user experience, and performance. 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'] def caesar(start_text, shift_amount, cipher_direction): end_text = "" if cipher_direction == "decode": shift_amount *= -1 for char in start_text: if char i...

Caesar Cipher V1 & V2(advanced)

Image
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")) def encrypt(plain_text, shift_amount): cipher_text = "" for letter in plain_text: ...

Prime number checker

Image
  def prime_checker(number): is_prime = True for i in range(2, number): if number % i == 0: is_prime = False if is_prime == True: print("It's a prime number.") else: print("It's not a prime number.") n = int(input("Check this number: ")) prime_checker(number=n)                                                                                

Paint Area Calculator

Image
You are painting a wall. The instructions on the paint can say that **1 can of paint can cover 5 square meters** of the wall. Given a random height and width of wall, calculate how many cans of paint you'll need to buy. the number of cans = (wall height ✖️ wall width) ÷ coverage per can.  But because you can't buy 0.6 of a can of paint, the **result should be rounded up** to **2** cans.  import math def paint_calc(height, width, cover): print(f"You need {math.ceil((height*width)/coverage)} cans") test_h = int(input("Height of wall: ")) test_w = int(input("Width of wall: ")) coverage = 5 paint_calc(height=test_h, width=test_w, cover=coverage)

Hangman - 3 other versions (advanced)

Image
  #Step 4 import random stages = [''' +---+ | | O | /|\ | / \ | | ========= ''', ''' +---+ | | O | /|\ | / | | ========= ''', ''' +---+ | | O | /|\ | | | ========= ''', ''' +---+ | | O | /| | | | =========''', ''' +---+ | | O | | | | | ========= ''', ''' +---+ | | O | | | | ========= ''', ''' +---+ | | | | | | ========= '''] end_of_game = False word_list = ["aardvark", "baboon", "camel", "flower", "singapore", "alex" ] chosen_word = random.choice(word_list) word_length = len(chosen_word) #TODO-1: - Create a variable called 'lives' to keep track of the number of lives left. #Set ...

Hangman - 3 versions

Image
#very simple code - V1 import random word_list = ["aardvark", "baboon", "camel", "myblog", "abs"] chosen_word = random.choice(word_list) guess = input("Guess a letter? >> ").lower() for letter in chosen_word: if letter == guess: print("Right!") else: print("Wrong!")   #much more advanced code - V2 import random word_list = ["aardvark", "baboon", "camel"] chosen_word = random.choice(word_list) #Testing code print(f'Pssst, the solution is {chosen_word}.') display = [] word_length = len(chosen_word) for _ in range(word_length): display += "_" guess = input("Guess a letter: ").lower() for position in range(word_length): letter = chosen_word[position] #print(f"Current position: {position}\n Current letter: {letter}\n Guessed letter: {guess}") if letter == guess: display[position] = letter print(display...

Escaping the Maze - Reeborg's world

Image
 def turn_right():     turn_left()     turn_left()     turn_left() while not at_goal():      if right_is_clear():         turn_right()         move()     elif front_is_clear():         move()     else:         turn_left() This code is not complete yet, will get back to it after day 15th.           

The hurdle loop challenge 3 - using "variable Hurdle" - Reeborg's world

Image
def turn_right():     turn_left()     turn_left()     turn_left()   def jump_long():     turn_left()     while wall_on_right():         move()     turn_right()     move()     turn_right()     while front_is_clear():         move()     turn_left()  while not at_goal():     if wall_in_front():         jump_long()     else:         move()  

The hurdle loop challenge 3 - using "while loops" - Reeborg's world

Image
 def turn_right():     turn_left()     turn_left()     turn_left() def jump():     turn_left()     move()     turn_right()     move()     turn_right()     move()     turn_left()   while not at_goal():     if wall_in_front():         jump()     else:         move()            

The hurdle loop challenge 2 - Moving hurdle - using "while loops" - Reeborg's world

Image
def turn_right():     turn_left()     turn_left()     turn_left() def jump():     move()     turn_left()     move()     turn_right()     move()     turn_right()     move()     turn_left() number_of_hurdles = 6 while number_of_hurdles > 0:     if at_goal():         pause()        else:         jump()         number_of_hurdles -= 1         print(number_of_hurdles) A simpler way to write this code would be: def turn_right():     turn_left()     turn_left()     turn_left() def jump():     move()     turn_left()     move()     turn_right()     move()     turn_right()     move()     turn_left()   while not at_goal():     jump() And we will get the sam...

The hurdle loop challenge 1 - using "while loops" - Reeborg's world

Image
def turn_right():     turn_left()     turn_left()     turn_left() def jump():     move()     turn_left()     move()     turn_right()     move()     turn_right()     move()     turn_left() number_of_hurdles = 6 while number_of_hurdles > 0:     jump()     number_of_hurdles -= 1  

The hurdle loop challenge 1 - using "for loops" - Reeborg's world

Image
 def turn_right():     turn_left()     turn_left()     turn_left() def jump():     move()     turn_left()     move()     turn_right()     move()     turn_right()     move()     turn_left() for steps in range(1, 7):     jump()