-1

I am trying to make some adjustments on a code where I want the 'key' to be multiple keys and every time put a key.

key = 'XXXXXXXXXXXXXXXXXXXX'
myfile = open("test.txt", "r")
for line in myfile:
    client = messagebird.Client(key)
    message = client.message_create(
          'idtest',
          (line),
          'Hi test',

        
      )

thats how i want the code to be like :

key1 = 'XXXXXXXXXXXXXXXXXXXX'
key2 = 'XXXXXXXXXXXXXXXXXXXX'
key3 = 'XXXXXXXXXXXXXXXXXXXX'
    myfile = open("test.txt", "r")
    for line in myfile:
        client = messagebird.Client(" KEY1 THEN HE PUT KEY2 like random everytime ")
        message = client.message_create(
              'idtest',
              (line),
              'Hi test',
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • I think yes! sorry i wasn't even able to form a question to google it lol – Jhon X Doe Oct 28 '21 at 21:46
  • You should probably take the time to go through some python tutorials. There's a whole bunch of decent ones a quick search away, and with some basic knowledge you know what to google for the more complex problems :) – Pranav Hosangadi Oct 28 '21 at 21:48

1 Answers1

1

You could put all your keys in a list then use the choice method from the random module to select a key at random

random.choice

Return a random element from the non-empty sequence seq. If seq is empty, raises IndexError

from random import choice
keys = ["key1", "key2", "key3"]
myfile = open("test.txt", "r")
for line in myfile:
    client = messagebird.Client(choice(keys))
    message = client.message_create(
        'idtest',
        (line),
        'Hi test',

Based on comments you can cycle through the keys by creating a cycle

itertools.cycle

Make an iterator returning elements from the iterable and saving a copy of each. When the iterable is exhausted, return elements from the saved copy. Repeats indefinitely

from itertools import cycle
keys = cycle(["key1", "key2", "key3"])
myfile = open("test.txt", "r")
for line in myfile:
    client = messagebird.Client(next(keys))
    message = client.message_create(
        'idtest',
        (line),
        'Hi test',
Chris Doyle
  • 10,703
  • 2
  • 23
  • 42