May-05-2017, 08:47 PM
(This post was last modified: May-05-2017, 09:38 PM by Liquid_Ocelot.)
Hey guys, I would just like to know how you import a random selection from a list that has been imported into python via a txt file as im trying to make like a dvd menu movie library thingy
I am trying to import a file from a folder then print a random title from the txt file but I get this strange output
here is my code
My bad I forgot to take out line 11 and also make line 16's string movies_list not file lol
Although I can now print the list it seems it still prints the whole contents of the txt file. Any idea how to print just one random item from the list rather than the whole lot?
I am trying to import a file from a folder then print a random title from the txt file but I get this strange output
here is my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import random print ( "***Movie Title Explorer***" ) print ( "l – load file of movie titles" '\n' "r – random movie" '\n' "s– search" '\n' "" "sw – starts with" '\n' "k – keep - save the last displayed movie title to your favourites" '\n' "f – favourites display" '\n' "" "c – clear" '\n' "q - quit program" ) print ( "command = ?" ) selection = input ( "Please Select:" ) if selection = = 'l' : file = open ( "C:\\Users\\LiquidO\\Desktop\\Assingment 2 python\\movies.txt" , "r" ) movies_list = file .readlines() # load each line into a list file .close() # close the file movies_list = [movie.strip() for movie in movies_list] print ( "Movies now loaded" ) selection = input ( "Please Select:" ) if selection = = 'r' : print (random.choice([ file ])) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
* * * Movie Title Explorer * * * l – load file of movie titles r – random movie s– search sw – starts with k – keep - save the last displayed movie title to your favourites f – favourites display c – clear q - quit program command = ? Please Select:l Movies now loaded Please Select:r <_io.TextIOWrapper name = 'C:\\Users\\LiquidO\\Desktop\\Assingment 2 python\\movies.txt' mode = 'r' encoding = 'cp1252' > Process finished with exit code 0 |
My bad I forgot to take out line 11 and also make line 16's string movies_list not file lol
1 |
file .close() # close the file |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import random print ( "***Movie Title Explorer***" ) print ( "l – load file of movie titles" '\n' "r – random movie" '\n' "s– search" '\n' "" "sw – starts with" '\n' "k – keep - save the last displayed movie title to your favourites" '\n' "f – favourites display" '\n' "" "c – clear" '\n' "q - quit program" ) print ( "command = ?" ) selection = input ( "Please Select:" ) if selection = = 'l' : file = open ( "C:\\Users\\LiquidO\\Desktop\\Assingment 2 python\\movies.txt" , "r" ) movies_list = file .readlines() # load each line into a list movies_list = [movie.strip() for movie in movies_list] print ( "Movies now loaded" ) selection = input ( "Please Select:" ) if selection = = 'r' : print (random.choice([movies_list])) |