Alex De Jesus
Alex De Jesus

Reputation: 19

Python - Need Help Getting User input stored in a list using python

I have this assignment where i have to create a list asking users to input 3 or 5 of their favorite movies, then im suppose to take that input and create a list with it and after display the list.

limit = 3

movieslist = []

while len(movieslist) < limit:  

    movie = raw_input("Enter The Name Of Your favorite Netflix movie" )
    print 
    movieslist.append(movie)

print "The Following Is A List Of Your Top 3 Favorite Netflix Movies:"

for x in movieslist:
    print x

Upvotes: 0

Views: 851

Answers (3)

Alex Hall
Alex Hall

Reputation: 36013

Here's a roundabout way if you want to troll/impress/confuse your professor:

def movie_generator():
    i = 0
    limit = 3
    while i < limit:
        i += 1
        yield raw_input("prompt")

movieslist = list(movie_generator())

Upvotes: 0

Alex Hall
Alex Hall

Reputation: 36013

Your professor's request is weird and I don't know if it'll satisfy her, but this'll work:

movieslist.extend([movie])

or equivalently:

movieslist += [movie]

This would also work but isn't using a while loop:

movieslist = [raw_input("....") for i in range(limit))]

Upvotes: 1

Scratch&#39;N&#39;Purr
Scratch&#39;N&#39;Purr

Reputation: 10399

You can use the insert method

print "In the Following Program Enter Your Top 10 Favorite Netflix Movies When Prompted"

print ""

limit = 10

movieslist = []

while len(movieslist) < limit:  

    movie = raw_input("Enter The Name Of Your Top Movie(s) From Netflix" )
    print 
    movieslist.insert(0,movie)

print "The Following Is A List Of Your Top 10 Favorite Netflix Movies:"

for x in movieslist:
    print x

Upvotes: 1

Related Questions