0

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
1
  • You can do without the empty print by inserting \n (the newline character) at the beginning or end of the raw_input prompt string, e.g. raw_input("Enter...Netflix\n") Commented May 2, 2016 at 19:24

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

This actually has different semantics to append since it inserts at the beginning, so the movies will appear in reverse order. The equivalent would be insert(len(movieslist) - 1, move). It looks weird, but so is disallowing append.
1

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))]

1 Comment

Yeah that makes no sense. All these answers are essentially tricks to get around using append, while append is the most basic operation for building up a list. Being expected to make a list with a variable number of elements without being taught append is nonsense.
0

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())

Comments

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.