0

I have 3 lists

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

y = ["4", "5", "6"]

z = ["7", "8", "9"]

I need to write into a file with a random from x, y, and z on to a new line each time.

Keyword = input("Directory to list")

with open(Keyword) as f: 
        content = f.readlines()

    content = [x.strip() for x in content]

    with open("test.txt") as w:
        w.write(PageFormat + )

Output should look like this:

2 // 6 // 8

3 // 4 // 9

1 // 5 // 9

2 // 5 // 9

1 // 4 // 7

("/" included)

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • You can use `random.choice(x)` in order to select a random value from the list `x`. – shriakhilc May 28 '19 at 04:04
  • You can refer [How to randomly select an item from a list](https://stackoverflow.com/questions/306400/how-to-randomly-select-an-item-from-a-list) – shriakhilc May 28 '19 at 04:05

2 Answers2

0

TRY:-

import random


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

y = ["4", "5", "6"]

z = ["7", "8", "9"]


file = open("new.txt",'w')

for a in range(0, 10):
    file.write(x[random.randrange(0,3)] + " // " + y[random.randrange(0,3)] + " // " + z[random.randrange(0,3)] + "\n")

file.close()

SAMPLE OUTPUT:-

2 // 4 // 9
1 // 6 // 9
2 // 5 // 9
1 // 6 // 8
1 // 5 // 8
3 // 5 // 9
3 // 4 // 7
2 // 6 // 7
1 // 5 // 7
2 // 4 // 8

You can control the number of iterations by changing the second argument of range(). This program will write 10 lines into a new file.

Vasu Deo.S
  • 1,820
  • 1
  • 11
  • 23
0
from random import choice

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

y = ["4", "5", "6"]

z = ["7", "8", "9"]

with open("test.txt", "w") as fp:
    fp.write(choice(x) + "//" + choice(y) + "//" + choice(z))
Vasu Deo.S
  • 1,820
  • 1
  • 11
  • 23
sha12
  • 1,497
  • 1
  • 17
  • 14