0

I have the following code for example:

n = ['321','243','780']
b = ['12','56','90']
a = ['178', '765', '111']

E = input('Enter Word...')
qw = 1
Code = ('')
E_ready = [E[no:no+qw] for no in range(0, len(E), qw)]
for code in E_Ready:
   letter = random.choice(code)
   Code += letter

If you enter the word 'nba' then it will output as 'nba', I want it to output with random elements from each letter's respective list so for example '32112178'

5
  • 4
    Actually you do not want that, that is really bad design: call-by-name is rather unsafe. You better use a dictionary. Commented Apr 1, 2017 at 20:23
  • 1
    But 'n' is not a list, it's a string; n is a list. Commented Apr 1, 2017 at 20:28
  • I was gonna use a dictionary but I really don't wanna use one, unless you can assign variables to lists in a dictionary. Commented Apr 1, 2017 at 20:30
  • I understand that it is handling 'code' as a list, so how can i make it take the value of code and use that Commented Apr 1, 2017 at 20:32
  • You should use a dictionary. Commented Apr 1, 2017 at 20:43

3 Answers 3

2

As Willem van Onsem correctly mentioned in the comments:

"...that is really bad design, call-by-name is rather unsafe. You better use a dictionary."

So, try this:

n = {'option1':'321','option2':'243','option3':'780'}
letter = random.choice(list(n.values()))

Or, shorter, as Chris has mentioned:

d = {'n':[321, 243, 780]}
letter = random.choice(d['n'])

Results from print(letter) (on both options):

321
321
780
243
etc.. 

EDIT:

How to add extra variables:

n = 'n'
d = {n:[321, 243, 780]}
letter = random.choice(d[n])
q = 'q'
d[q] = [600, 234, 180]
new_letter = random.choice(d[q])

Now print(new_letter) gives:

234
180
180
600
etc..

SECOND EDIT (which btw is pure bonus since the question turned into a completely different one then first asked.. therefor, it is left unoptimized. Yet, working nonetheless..):

import random
d = {'n':[321, 243, 780], 'b':['12','56','90'], 'a':['178', '765', '111']}

E = input('Enter Word...')

inputword = list(E)

for key in d.keys():
    if key in inputword:
        for i in range(len(inputword)):
            if inputword[i] == key:
                try:
                    inputword[i] = str(random.choice(d[key]))
                except:
                    pass

result = ''.join(inputword)
print(result)

If input = nba then output = 32190111

If input = nhhsygbbvvra then output = 321hhsyg5690vvr178

Etc..

Sign up to request clarification or add additional context in comments.

8 Comments

oh, I think I can make this work, I'll try it out and reply if it works.
I still want to use a variable bec. I'm gonna have multiple lists/dictionaries so i need to use sth like this random.choice(list(VariableName.values()))
Could you update your question with the adapted code you are trying to execute now and the exact error it gives you?
I just did, hopefully it helps
so how do i implement this exactly?
|
1

Okay, you have a few fundamental issues. When you want to assign one variable to another then you wouldn't put it in quotes. So it should be:

code = n

But actually I'm wondering why you need the variable code at all. You could simply do

import random
code = [ '321', '243', '780' ]
letter = random.choice(code)
print letter

4 Comments

I can't do that bec. in the bigger picture, I will be taking code from a for loop so i want it to take a random element from different lists.
See Willem van Onsem's comment to the original question, call-by-name is bad design..
like i.e i have 'a', 'b', 'c' lists, I made a for loop that will return 'a' , 'b' or 'c' so i want it to take a random element out of the respective list.
@YousefRadwan you really should not do that.
1

I agree with the comment, it is better to use something like:

d = {'n':[mylist]}
letter = random.choice(d['n'])

the problem is that random.choice works on strings too... it simply considers them to be a list of characters. in your case the code variable and n are supposed to be exactly the same but are in fact not. You could also do

n = [list]
code = n ## only makes any sense, if there 
         ## is a procedure assigns it based on some condition 
letter = random.choice(code)

Comments

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.