-2

I don't know if this is possible (new to python), but I want to use a list of defined variables, and when I run in it shell or cmd to accept input then display the translated text.

For an example:

a = "00";
aa = " 0000";
b = "11";
bb =" 1111";
zz =" ";

Then if the client inputs: a,b,bb,aa,zz,a,b

I want it to display: 0011 1111 0000 0011 (just like that)

I would like to stick to the current way of assigning variables if there are other ways.

  • 1
    why do you want to do this?. Try to use json data types for these kind of things – Arpit Solanki Jun 11 '17 at 17:54
  • I want to make a not-too-secure encryption method, which will use randomized unicode characters to decrypt and encrypt data types (mainly working somewhat like a JPEG files, patterns not listed will be left alone, while listed patterns will be modified. Each person will have a key of variables that is generally their responsibility not to publish to others – Andrew Chon Jun 11 '17 at 18:00
  • possible duplicate of [Python 3 get value of variable entered from user input](https://stackoverflow.com/q/15168765) – jscs Jun 11 '17 at 18:02
  • "I want to make a not-too-secure encryption method", well, you're on the right track for the "not-too-secure" part. Seriously, this isn't secure _at all_. _Never_ roll your own encryption. It's **_shockingly_** difficult to get right. – ChrisGPT was on strike Jun 11 '17 at 18:03
  • Its just a concept... The program would be client side so it would be secure as long as they didn't tinker around with code and didn't purposefully release their variables, which they shouldn't know in the first place – Andrew Chon Jun 11 '17 at 18:06
  • 1
    @AndrewChon, maybe you don't understand how naive that attitude is. It's simply wrongheaded. Again, _**never** try to roll your own encryption_. It **won't be secure**. Read up on [security through obscurity](https://en.wikipedia.org/wiki/Security_through_obscurity), paying particular attention to the "criticism" section. If that doesn't convince you, read an introductory text on encryption. If _that_ doesn't convince you, do some research on failed proprietary encryption algorithms. If after doing all of that you still want to roll your own crypto you can't be helped. – ChrisGPT was on strike Jun 11 '17 at 18:09
  • 1
    @Chris I'm going to have to disagree with "Never try to roll your own encryption". That's just bad advice. "Never use encryption you wrote in production code" would be a better suggestion. Writing encryption code is a fun exercise. – Carcigenicate Jun 11 '17 at 18:18
  • @Carcigenicate, as an educational exercise I agree that it can be useful. (Comments have limited length. But I don't agree that it's "bad advice". It's mostly good advice.) The OP clearly lacks any knowledge of crypto fundamentals or even of the level of complexity involved. And nothing here suggests this is a learning exercise. OP, this is still a horrible idea, even if it's for fun or education. Take a course or read a book instead of trying to make it up yourself. Mapping characters to sequences of numbers is basically a Caesar cipher; children play with them. – ChrisGPT was on strike Jun 11 '17 at 18:25

2 Answers2

0

I would recommend using a dictionary for this, like so:

translations = {
    'a' : '00',
    'aa' : '0000',
    'b' : '11',
    'bb' :'1111',
    'zz' : ''
}

# python3
userInput = input()
print(' '.join(translations[key.strip()] for key in userInput.split(',')))

# python2
userInput = raw_input()
print ' '.join(translations[key.strip()] for key in userInput.split(','))

Be aware that this will raise a KeyError if the user inputs an undefined key. You can wrap the dict lookup in a try-except block and catch the KeyError to deal with this issue. Alternatively, you can use the dictionary's .get() method which allows you to provide a default value as the second argument.

Will Da Silva
  • 6,386
  • 2
  • 27
  • 52
  • Thank you I will try this out – Andrew Chon Jun 11 '17 at 18:02
  • It says they are not defined? I inputted a,aa and this is what happened: `Traceback (most recent call last): File "test.py", line 9, in userInput = input() File "", line 1, in NameError: name 'b' is not define` – Andrew Chon Jun 11 '17 at 18:03
  • Are you using Python 2, or Python 3? The code I wrote in the answer will only work on Python 3, which is what you should be using unless you have an extremely compelling reason not to. For python 2, replace `input()` with `raw_input()`. – Will Da Silva Jun 11 '17 at 18:06
  • I am learning Python 2.7 on Lynda, ill get Python 3 instead sorry I am new to this. BTW they said most people use 2? – Andrew Chon Jun 11 '17 at 18:07
  • support for python 2 ends in 2020. – Gahan Jun 11 '17 at 18:09
  • still far away. – Arpit Solanki Jun 11 '17 at 18:10
  • Oh. Thank you for telling me. IDK when the Lynda tutorials were made but probably nowhere near this year – Andrew Chon Jun 11 '17 at 18:11
  • Another error: `C:\Users\andrc\Documents\1337>py test.py File "test.py", line 10 print ' '.join(translations[key.strip()] for key in userInput.split(',')) ^ SyntaxError: invalid syntax` Do I need to put something there? – Andrew Chon Jun 11 '17 at 18:19
0

It is probably best to achieve this with a dictionary you define yourself. But if as you say, you don't want to change how you're working with those variables, you could also make use of locals() which gives you access to a dictionary of defined local variables. To give you a fully worked-out example:

a = "00"
aa = " 0000"
b = "11"
bb =" 1111"
zz =" "
variables = input("Variable string? ")
# Process the input into variable names
variables = [variable_.strip() for variable_ in variables.split(",")]
# Find values assigned to each variable
text = []
for variable_ in variables:
    try:
        text.append(str(locals()[variable_]))
    except KeyError:
        print("Variable {} not found".format(variable_))
print("".join(text))
wphicks
  • 355
  • 2
  • 9