Skip to main content
The util module provides utility functions for interacting with users through the command-line interface, including input prompts and formatted output display.

pedirmsj

Prompts the user to enter a message and returns it in uppercase format.
def pedirmsj(entrada: str)
entrada
str
required
The prompt text to display to the user when requesting input.
return
str
The user’s input converted to uppercase. This ensures consistency with the alphabet mappings used in the encryption modules.
The function automatically converts input to uppercase to match the Spanish alphabet dictionaries used in the sustitucion module (A-Z, Ñ).

How it works

  1. Displays the provided prompt to the user
  2. Reads the user’s input
  3. Converts the input to uppercase
  4. Prints a confirmation message showing the entered message
  5. Returns the uppercase message

Example

import util

# Prompt user for a message
mensaje = util.pedirmsj("Ingrese el mensaje a operar: ")
print(f"You entered: {mensaje}")

# Example interaction:
# > Ingrese el mensaje a operar: hola mundo
# El mensaje es: HOLA MUNDO

Real-world usage

import util
import sustitucion

def main():
    # Get message from user
    mensaje = util.pedirmsj("Ingrese el mensaje a operar: ")
    print(f'Mensaje original: {mensaje}')
    
    # Use the message for encryption
    pos = 8
    cifrado = sustitucion.convertiraNumero(mensaje)
    cifrado = sustitucion.cifrar(cifrado, pos)
    mensajecifrado = sustitucion.convertiraLetra(cifrado)

printResultado

Displays the result of a cryptographic operation (encryption or decryption) with formatted output and a brief pause.
def printResultado(operacion, mensaje)
operacion
str
required
The type of operation performed. Typically “cifrado” (encrypted) or “descifrado” (decrypted).
mensaje
str
required
The result message to display to the user.
return
None
This function does not return a value. It prints the result and pauses for 1 second.
The function includes a 1-second sleep after displaying the result, which helps create a better user experience by preventing output from scrolling too quickly in batch operations.

How it works

  1. Formats and prints the operation result in a user-friendly format
  2. Pauses execution for 1 second using time.sleep(1)
  3. This allows the user to read the result before the next operation

Example

import util

mensaje_cifrado = "PWTM"
util.printResultado("cifrado", mensaje_cifrado)
# Output: "El mensaje cifrado es: PWTM"
# Then waits 1 second

Real-world usage

import sustitucion
import util

def sustitucion_cifrar(mensaje, pos):
    cifrado = sustitucion.convertiraNumero(mensaje)
    cifrado = sustitucion.cifrar(cifrado, pos)
    mensajecifrado = sustitucion.convertiraLetra(cifrado)
    util.printResultado("cifrado", mensajecifrado)
    return mensajecifrado

def sustitucion_descifrar(mensaje_cifrado, pos):
    mensaje_cifrado = sustitucion.convertiraNumero(mensaje_cifrado)
    descifrado = sustitucion.descifrar(mensaje_cifrado, pos)
    mensajedescifrado = sustitucion.convertiraLetra(descifrado)
    util.printResultado("descifrado", mensajedescifrado)
    return mensajedescifrado

Complete utility workflow

Here’s a complete example showing how to use both utility functions together:
import util
import sustitucion

# Step 1: Get input from user
mensaje = util.pedirmsj("Ingrese el mensaje a cifrar: ")

# Step 2: Perform encryption
pos = 8
cifrado = sustitucion.convertiraNumero(mensaje)
cifrado = sustitucion.cifrar(cifrado, pos)
mensaje_cifrado = sustitucion.convertiraLetra(cifrado)

# Step 3: Display result
util.printResultado("cifrado", mensaje_cifrado)

# Step 4: Get encrypted message for decryption
mensaje_para_descifrar = util.pedirmsj("Ingrese el mensaje a descifrar: ")

# Step 5: Perform decryption
numeros = sustitucion.convertiraNumero(mensaje_para_descifrar)
descifrado = sustitucion.descifrar(numeros, pos)
mensaje_descifrado = sustitucion.convertiraLetra(descifrado)

# Step 6: Display result
util.printResultado("descifrado", mensaje_descifrado)

Module dependencies

The util module imports and uses:
  • time - Python standard library for the sleep functionality in printResultado

Usage in other modules

The utility functions are primarily used in:
  • main.py - For command-line interface interactions
  • Custom scripts that need user input/output formatting
These utility functions are designed for command-line applications. For web applications (like the Flask app in app.py), these functions are not used since input/output is handled through HTML forms and templates.

Customization examples

You can build on these utilities for enhanced functionality:
import util

def pedirNumero(entrada: str):
    while True:
        valor = util.pedirmsj(entrada)
        try:
            return int(valor)
        except ValueError:
            print("Por favor, ingrese un número válido.")

# Usage
clave = pedirNumero("Ingrese la clave numérica: ")

Build docs developers (and LLMs) love