What's new

Help Python code

BrigthWin

Forum Veteran
Elite
Joined
Jul 21, 2020
Posts
1,638
Reaction
1,768
Points
650
Using filestream
 
Last edited:
To solve the given problem, you can use the following Python code that reads the input from the file proba.in, encrypts the messages using the Caesar Cipher encryption technique, and prints the encrypted messages:

Python:
def caesar_cipher(message, key):
    result = ""
    for char in message:
        if char.isalpha():
            shift = 65 if char.isupper() else 97
            result += chr((ord(char) - shift + key) % 26 + shift)
        else:
            result += char
    return result

with open('proba.in', 'r') as file:
    lines = file.readlines()
    n = int(lines[0].strip())
    
    if n < 1 or n > 5:
        print("Invalid Counter!")
    else:
        for i in range(1, n+1):
            data = lines[i].strip().split()
            if len(data) != 2 or not data[1].isdigit():
                print("Invalid Input!")
            else:
                message, key = data[0], int(data[1])
                encrypted_message = caesar_cipher(message, key)
                print(encrypted_message)

You can paste this code into a Python script file and run it to read the input from the proba.in file and encrypt the messages using the Caesar Cipher encryption technique as described in the problem statement.

If you have any specific questions or need further clarification, feel free to ask!
 
Back
Top