0

Ive been searching but I just cant find anything that describes my problem. Im just learning python so I might not even know how to properly phrase the question.

Im trying to randomize a selection of defined variables, but I can not figure out how to retrieve those variables. example:

import random

user1 = "usernamehere1"
userkey1 = "3097fds09aj4023jr30mf2ag2"
user2 = "usernamehere2"
userkey2 = "09asfh34907fsenk32498fgg9"
user3 = "usernamehere3"
userkey3 = "234kn34bnero8wn34lnkjwi34"

numbers =  ["1", "2", "3"]

user_number = random.choice(numbers)
user = "user" + user_number
wif = "userkey" + user_number

print(user)
print(wif)

Instead of getting: (say if it selects "2" as the random number):

  • usernamehere2
  • 09asfh34907fsenk32498fgg9

I just get:

  • user2
  • userkey2

Any guesses as to what am I doing wrong?

4 Answers4

1

look at this post To convert string to variable name, You can do what you want using exec to change strings to a variable, but this is not safe and definitely not recommended. As the post here explains you should use dictionaries instead to do this such as users["user" + user_number]

import random

user1 = "usernamehere1"
userkey1 = "3097fds09aj4023jr30mf2ag2"
user2 = "usernamehere2"
userkey2 = "09asfh34907fsenk32498fgg9"
user3 = "usernamehere3"
userkey3 = "234kn34bnero8wn34lnkjwi34"

dict = {}
dict[user1] = userkey1
dict[user2] = userkey2
dict[user3] = userkey3

numbers =  ["1", "2", "3"]

user_number = random.choice(numbers)
user = "user" + user_number

print(user)
print(dict[user])
Community
  • 1
  • 1
James Russo
  • 578
  • 3
  • 18
  • Thanks. I am reading up on dictionaries now but it leads me to the same question. I dont understand if each keypair needs its own dictionary or how I would randomize them. – Don Fantasia Jul 27 '16 at 18:04
  • it depends how you want to store the information, you can either separate it out like other users have in their answers or do as I showed above where username is the key of the dictionary and the userkey is the value in the dictionary. It's a design choice – James Russo Jul 27 '16 at 18:06
0

The comments are correct, I just wanted to share a way to implement them. You can think of dictionaries as "look up table" where you assign a key-->value pair. There is a lot of info here

import random

users = {"1":"usernamehere1",
         "2":"usernamehere2",
         "3":"usernamehere3"}

keys = {"1":"3097fds09aj4023jr30mf2ag2",
        "2":"09asfh34907fsenk32498fgg9",
        "3":"234kn34bnero8wn34lnkjwi34"}

numbers =  ["1", "2", "3"]

user_number = random.choice(numbers)
user = "user" + users[user_number]
wif = "userkey" + keys[user_number]

print(user)
print(wif)
mitoRibo
  • 4,468
  • 1
  • 13
  • 22
  • AWESOME! Im going to give this a shot. Im the type to learn with actual examples and I greatly appreciate it! – Don Fantasia Jul 27 '16 at 18:07
  • Just wanted to say I was able to implement this for all 100 keypairs and it worked amazing. Now Im re-reading everything in order to gain a basic understanding of dictionaries. Very grateful for your help. – Don Fantasia Jul 27 '16 at 18:24
0
import random
users = [
        {
            'user':"usernamehere1",
            'userkey':"3097fds09aj4023jr30mf2ag2"
        },
        {
            'user':"usernamehere2",
            'userkey':"09asfh34907fsenk32498fgg9"
        },
        {
            'user':"usernamehere3",
            'userkey':"234kn34bnero8wn34lnkjwi34"
        }
    ]

user_number = random.choice(range(1,len(users)))

print(users[user_number]['user'])
print(users[user_number]['userkey'])
Bryce Drew
  • 5,777
  • 1
  • 15
  • 27
0

You can use this with just one dictionary:

import random

users = {
    "usernamehere1": "3097fds09aj4023jr30mf2ag2",
    "usernamehere2": "09asfh34907fsenk32498fgg9",
    "usernamehere3": "234kn34bnero8wn34lnkjwi34"
}

user = random.sample(users.keys(), 1)

print(user[0])
print(users[user[0]])
Rafiqul Hasan
  • 488
  • 4
  • 12
  • This works great! How do I assign user and wif? For instance: user = user[0] wif = users[user[0]] – Don Fantasia Jul 27 '16 at 19:09
  • For this to work to assign variables: `keypair = random.sample(users.keys(), 1)` `user = keypair[0]` `wif = users[keypair[0]]` `print(user)` `print(wif)` – Don Fantasia Jul 27 '16 at 19:29