lundi 8 octobre 2018

How do I allow Python to choose a random line from 2 text files (that are the same line) and then store them as variables?

So, I have an issue that I have been trying to solve countless times over the past few hours which is:

I have been trying to have the program pick a certain line in a text file, and once it has done that, it needs to use the line number that it has selected to use in another file.

So let's say that the file is called "a.txt" and the other file is called "b.txt", I need the program to pick a random line (1,50) and then display the line it picked out, but then it needs to do the same with the other file, so that the same line number is picked in both files.

The code that I currently have:

import random

with open("a.txt") as word_file:
    words = word_file.read().split()
    randomw = random.choice(words)


with open("b.txt") as artist_file:
    words = artist_file.read().split()
    randname=random.choice(words)

print(randomw +" "+ randname)

Thanks for any help!




Aucun commentaire:

Enregistrer un commentaire