Featured Posts

CS50 Python , Nutrition Facts Table

Image
Nutrition Facts: Python Practice for Beginners Nutrition Facts Table for Python Practice Welcome to this comprehensive guide for Python beginners! If you are learning how to work with lists, dictionaries, and loops, this post will help you build practical skills using a real-world example: nutrition facts for fruits. Understanding how to organize and manipulate data is a key part of programming, and this exercise will give you hands-on experience. Below is a sample table of fruits and their calorie values, formatted as a Python list of dictionaries. This structure is ideal for coding exercises, projects, or even building your own nutrition calculator. You can expand this list, add new fruits, or use it as a foundation for more advanced Python tasks. Python List of Dictionaries Example: fruits = [ {'name': 'Apple', 'calories': 130}, {'name': 'Avocado', 'calories': 50}, {'name': 'Banana', 'ca...

Python LAB 2.5.1.6 Improving the Caesar cipher

 Improving the Caesar cipher

2.5.1.6 LAB: Improving the Caesar cipher
Improving the Caesar Cipher

Level of difficulty

Hard

Pre-requisites

Module 1.11.1.1, Module 1.11.1.2

Objectives

  • improving the student's skills in operating with strings;
  • converting characters into ASCII code, and vice versa.

Scenario

You are already familiar with the Caesar cipher, and this is why we want you to improve the code we showed you recently.
The original Caesar cipher shifts each character by one: a becomes b, z becomes a, and so on. Let's make it a bit harder, and allow the shifted value to come from the range 1..25 inclusive.
Moreover, let the code preserve the letters' case (lower-case letters will remain lower-case) and all non-alphabetical characters should remain untouched.
Your task is to write a program which:
  • asks the user for one line of text to encrypt;
  • asks the user for a shift value (an integer number from the range 1..25 - note: you should force the user to enter a valid shift value (don't give up and don't let bad data fool you!)
  • prints out the encoded text.
Test your code using the data we've provided.
Test data

Sample input:

abcxyzABCxyz 123
2

Sample output:

cdezabCDEzab 123

Sample input:

The die is cast
25

Sample output:

Sgd chd hr bzrs

Solution Code:

Now, if you are looking for a ready code, below you will find it (but coding is not about ready solutions). And if you are looking forward to learn and enhance your python coding skills and knowledge, please let me know in the comments and I will tell you how this code has been built.

text = input("Enter your message: ")
shift_value = int(input("Please Enter a Shift Value from 1 - 25: "))

def encryped(text , shift_value):
    cipher = ''
    for char in text:
        if char == ' ':
            cipher += char
        elif char.isdigit():
            cipher += char
        elif char.isupper():
            cipher += chr((ord(char) + shift_value - 65) % 26 + 65)
        else:
            cipher += chr((ord(char) + shift_value - 97) % 26 + 97)
    return cipher
print("The Encrypted string is:\n ", encryped(text, shift_value))

Follow our python page for more

citation

Python, K. I. (2019, October 14). Encryption of message with ceasor cipher algorithm using python. YouTube. Retrieved January 27, 2022, from https://www.youtube.com/watch?v=Ws5E2gCW4Hc&feature=youtu.be

Comments

Popular posts from this blog

فرصتك للدراسة في ألمانيا: منحة ممولة بالكامل لطلاب الدراسات العليا

How to Deactivate Screen Reader in Kali Linux

Murphy's Law: Expect the Unexpected

Python 3.2.1.14 LAB: Essentials of the while loop

Data Analysis Roadmap 2026: From Excel Lover to Python-Powered Analyst