Posts

Showing posts with the label python

Secret Auction

Image
  Adding to a list in Python Adding to a dictionary in Python #Find mere here: https://my-python-journey.blogspot.com/ from replit import clear #HINT: You can call clear() to clear the output in the console. from art import logo print(logo) print("Blind Auction Program V1") bids = {} bidding_finished = False def find_highest_bidder(bidding_record): highest_bid = 0 winner = "" # bidding_record = {"Angela": 123, "James": 321} for bidder in bidding_record: bid_amount = bidding_record[bidder] if bid_amount > highest_bid: highest_bid = bid_amount winner = bidder print(f"The winner is {winner} with a bid of ${highest_bid}") while not bidding_finished: name = input("What is your name?: ") price = int(input("What is your bid?: $")) bids[name] = price should_continue = input("Are there any other bidders? Type 'yes or 'no'.\n") if should_continue == "no...

Travel Log (nested dictionaries in a list)

Image
travel_log = [ { "country": "France", "visits": 12, "cities": ["Paris", "Lille", "Dijon"] }, { "country": "Germany", "visits": 5, "cities": ["Berlin", "Hamburg", "Stuttgart"] }, ] #TODO: Write the function that will allow new countries to be added to the travel_log. def add_new_country(country_visited, time_visited, cities_visited): new_country = {} new_country["country"] = country_visited new_country["visits"] = time_visited new_country["cities"] = cities_visited travel_log.append(new_country) add_new_country("Russia", 2, ["Moscow", "Saint Petersburg"]) print(travel_log)  

Nesting in directories and lists

  ##Python Dictionaries How to add to a list in Python How to add to a dictionary in Python programming_dictionary = { "Bug": "An error in a program that prevents the program from running as expected.", "Function": "A piece of code that you can easily call over and over again.", } #Retrieving items from dictionary. # print(programming_dictionary["Function"]) #Adding new items to dictionary. programming_dictionary["Loop"] = "The action of doing something over and over again." #Create an empty dictionary. empty_dictionary = {} #Wipe an existing dictionary # programming_dictionary = {} # print(programming_dictionary) #Edit an item in a dictionary programming_dictionary["Bug"] = "A moth in your computer." # print(programming_dictionary) #Loop through a dictionary # for key in programming_dictionary: # print(key) # print(programming_dictionary[key]) ####################################### #Nest...

Converting student scores to grades (Dictionaries)

Image
You have access to a database of `student_scores` in the format of a dictionary. The **keys** in `student_scores` are the **names** of the students and the **values** are their exam **scores**.  Write a program that **converts their scores to grades**. By the end of your program, you should have a new dictionary called `student_grades` that should contain student **names** for **keys** and their **grades** for **values**. T**he final version** of the `student_grades` dictionary will be checked. student_scores = { "Harry": 81, "Ron": 78, "Hermione": 99, "Draco": 74, "Neville": 62, } #TODO-1: Create an empty dictionary called student_grades. student_grades = {} #TODO-2: Write your code below to add the grades to student_grades.👇 for student in student_scores: score = student_scores[student] if score > 90: student_grades[student] = "Outstanding" elif 80 < score < 91: student_grades[student] = ...

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()

Password Generator Project 2 - (Advanced)

Image
  import random letters = ['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'] numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+'] print("Welcome to t...

Password Generator Project 1 - Easy Code

Image
  #Password Generator Project import random letters = ['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'] numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+...

FizzBuzz

Image
  for number in range(1, 101): if number % 3 == 0 and number % 5 == 0: print("FizzBuzz") elif number % 3 == 0: print("Fizz") elif number % 5 == 0: print("Buzz") else: print(number)

Students Average Height

Image
student_heights = input("Input a list of student heights>> " ).split() for n in range(0, len(student_heights)): student_heights[n] = int(student_heights[n]) #b = round(sum(student_heights)/len(student_heights)) #print(b) #print(student_heights) #m = len(student_heights) #t = student_heights[0] + student_heights[1] + student_heights[2] #print(round(t/m)) number_of_students = 0 for student in student_heights: number_of_students += 1 total_heights = 0 for height in student_heights: total_heights += height print(round(total_heights / number_of_students))