Featured Posts

فرصة تطوعية مميزة لطلاب وطالبات الجامعة والمهتمين بالعمل الحر

Image
حُر HURR LEAP 2026 فرصة تطوعية حصرية فرصة تطوعية مميزة لطلاب وطالبات الجامعة  والمهتمين بالعمل الحر Leap 2026 - hurr ٧٥ سفير فقط يمثلون منصة حُر في أكبر حدث تقني بالمنطقة 📍 المكان ملهم، الرياض 🗓️ التاريخ 31 أغسطس – 3 سبتمبر وش بتستفيد كسفير لـ "حُر"؟ 🎟️ تذكرة دخول طوال فترة المعرض. 💼 الحصول على مشاريع حقيقية مباشرة من زوار المعرض. 📜 شهادة تطوع معتمدة. 🏅 باجات رسمية باسم "سفير حُر". 👑 اشتراك مميز مجاني في المنصة. 🎓 ورشة تدريبية مجانية للتحضير قبل الحدث. 📝 خطاب خبرة وتوصية للمتميزين. 🏆 جوائز يومية لأفضل السُفراء. 🚌 المواصلات مؤمّنة الذهاب: باص كل 15 دقيقة من محطة مترو SAB (من 12 ظهراً حتى 5 عصراً). العودة: إلى محطة مترو SAB (من 3 عصراً حتى 10 مساءً). انضم الآن وسجّل كسفير ← ⚠️ المقاعد محدودة والتسجيل يخضع للمفاضلة.

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