0

I am a beginner python learner. I was wondering if it was possible to create a basic massage encrypting system without using any modules. I want my programm to check and use variable name that is in string.

let's say,

a = 'l'
b = '5'
c = 'o'

x = input("Enter your massage: ")

print(x, 'THE_USER INPUT matching the variable name and values')

I know I can do this with while or if, but it would take forever. Also, how do you separate each string letters before you match the variable.

I am using python 3. Thanks :)

2
  • 1
    its not clear what you are trying to do... Commented Sep 21, 2012 at 23:39
  • I was equally confused when I wrote this, sorry. Commented Sep 22, 2012 at 0:07

3 Answers 3

2

I have no idea if this is what you are trying to do, but here goes:

a = 'l'
b = '5'
c = 'o'

x = input("Enter your message: ")
values = [globals().get(var, '') for var in list(x)]

print "".join(values)

EXAMPLE

Enter your message: abc
l5o

A more appropriate way to do this would likely be:

replacements = { 'a': 'l', 'b': '5', 'c': 'o' }

x = input("Enter your message: ")
print "".join([replacements.get(val, "") for val in x])
Sign up to request clarification or add additional context in comments.

4 Comments

on a side note while this is what the OP asked for this is probably not really what they should be doing...
Completely and totally agree.
your more appropriate way is a significant improvement. .. but really he should be using string.translate for this ...
Thanks This is exactly what I was looking for. I will now have to check out references to understand the new syntax. Thanks :)
1

use string.translate this is a much more correct way to do what it sounds like you want

from string import maketrans

in_tab  = "abcdefghijklmnopqrstuvwxyz "
out_tab = "5QR&*(=-;Wwz%$^#@!yY~:123xq"

t = maketrans(in_tab,out_tab)
print ("Hello World".translate(t))

1 Comment

This is pretty much what I wanted to do, but i don't want any help from any modules. Thanks, I didn't know I could do that. I will use this methods in future :)
1

In case you meant substituting every occurrence of a given character by another one, have a look at the str.translate function.

You could extend your example code as follows:

import string

in = input("Enter your message: ")
mapping = string.maketrans('abc', '15o')
out = in.translate(mapping)
print(out)

Every a is substituted by a 1, every b by 5, every c by o.

3 Comments

Thanks, but I was looking for a method that works without importing any module. I think this requires module. But it seems easier and I will use it later on :)
You are right, added the missing import. But using modules is nothing to avoid! Do not try to reinvent the wheel :) Especially not for such common and tedious/error-prone tasks as string manipulation.
BTW: The import is just necessary for the maketrans helper function, it just builds a 256 character-long string for you that you could specify yourself. Not that anybody would want to do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.