MissSprezzatura
MissSprezzatura

Reputation: 313

Substitution Caesar Cipher String Error

This program will encrypt a message. It will be a Caesar version of a Substitution encrypted message. It will shift the letters by 5.

from string import *

original = raw_input("Enter your string: ") # prompts user for their desired string

def sub(input, x):
    alpha = string.ascii_lowercase
    newAlpha = alpha[x:] + alpha[:x]
    i = string.maketrans(alpha, newAlpha)
    return input.translate(i)

print sub(original, 5)

I'm not sure why there is

NameError: global name 'string' is not defined

Upvotes: 0

Views: 75

Answers (2)

Anil_M
Anil_M

Reputation: 11473

You are importing everything from string module using from string import * and then again calling string top level module at string.maketrans and string.ascii_lowercase

Just use maketrans(alpha, newAlpha) and ascii_lowercase

Ideally importing everything from a module is not a good idea and best practice. You are cluttering namespace. Just import what you need or import high-level module.
for e.g.
from string import maketrans,ascii_lowercase
and then use
i = maketrans(alpha, newAlpha)
alpha = ascii_lowercase

or

import string
and then use
i = string.maketrans(alpha, newAlpha)
alpha = string.ascii_lowercase

Upvotes: 1

that_guy
that_guy

Reputation: 21

Use import string instead of from string import * for compatibility of the sub function with your importing method.

Upvotes: 2

Related Questions