4

I want to create a list from a user's input and then return a random value from that list.

This is what I have so far for the code but I can't figure out how to get input for the array.

import random
movie_array = ["movieA", "movieB", "movieC"]
print(random.choice(movie_array))

I know about the input function but I'm not sure how to use it to create an array/list. I tried the following but it didn't work

movie_array = input()

but when I run the random.choice on that it only selects an individual character.

4
  • Try opening a scanner and asking the user to first input the number of elements in the array (size for array initialization), then use the same process to take n inputs. Commented Feb 8, 2016 at 3:21
  • 2
    @DebosmitRay: what is a Scanner? This is a question about Python, not Java. Commented Feb 8, 2016 at 3:23
  • This shows how to put user input into arrays: stackoverflow.com/questions/27675035/… Commented Feb 8, 2016 at 3:26
  • Please excuse the use of the word scanner. Python has this input function that you can use. Commented Feb 8, 2016 at 3:27

5 Answers 5

3

You can do it like this:

>>> import random
>>> movie_array = [input("Input a movie: ") for i in  range(3)]
Input a movie: movieA
Input a movie: movieB
Input a movie: movieC
>>> print(random.choice(movie_array))
movieC
Sign up to request clarification or add additional context in comments.

Comments

1

Use a loop.

In the body of the loop prompt the user to enter a movie title, or 'q' to quit. Append each entry to your movie list. When finished select a random movie from the movie list:

import random

movies = []
while True:
    entry = input('Enter a movie title (q to quit): ')
    if entry.lower() == 'q':
        break
    movies.append(entry)

if movies:
    print(random.choice(movies))

Hopefully your users do not want to enter the movie entitled "Q" (1982). It might be better to use an empty entry as the sentinel, like this:

    entry = input('Enter a movie title (<Enter> to quit): ')
    if entry == '':
        break

Comments

0

You can read movies in a single line separated by commas and then split it into array.

movie_array=raw_input("Enter Movies").split(',')

If it is python 3.x

movie_array=input("Enter Movies").split(',')

2 Comments

Many, many, if not most movie titles consist of more than one space separated word. Perhaps if the movies were separated with a comma instead?
Yes. I almost forgot that.
0

Just want to clarify why your solution attempt didnt work IE this:

movie_array = input()

It's because the input returns a string I.E a list of substrings so you can seemovie_array = 'test' asmovie_array = ['t','e','s','t']

print(movie_array[1])
>>>'e'

in order to solve your problem I would recommend @mhawke answer (using a while loop)

By using a while loop you can retrieve multiple inputs and store each of them in a list. Be aware though that a while loop has to be able to "END" or it can cause problems. Therefore you have to have a break somewhere in the while loop. Otherwise the program would be run indefinetly until you either forced it to shutdown or until it crashes.

Further information about this can be read here:

also checkout the official wiki for strings and control flow.

Comments

-1

You can give it like this.

movie_array = [b for b in input().split()]

And then you can get input from user like this,

movieA movieB movieC

1 Comment

1. The list comprehension is pointless - input().split() already returns a list. 2. What if the user wanted to enter a movie title such as "Star Wars"? Splitting on whitespace will split that title into two - Star and Wars.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.