Write a Python program that will ask the user for the name of a movie. Add the movie entered to a list. Continue asking for a movie until the user enters ‘0’. After all movies have been input, output the list of movies one movie per line.
This is what I’ve tried:
JavaScript
x
14
14
1
def main():
2
movies = []
3
while movies != 0:
4
movie = str(input("Enter the name of a movie: "))
5
if movie == 0:
6
break
7
if movie != 0:
8
movies.append(movie)
9
10
print("That's your list")
11
print(movies)
12
13
main()
14
Advertisement
Answer
Use the break
keyword to interrupt either a while
or a for
loop.
The code given by BuddyBob is not 100% correct because it will include the “0” in the list of movies (since it is first appended). The code given by ALI NAQI is actually comparing with a lowercase ‘o’.
I believe this would be the best way:
JavaScript
1
14
14
1
def main():
2
movies = []
3
while True:
4
movie = input("Enter the name of a movie: ")
5
if movie == "0":
6
break
7
else:
8
movies.append(movie)
9
10
print("That's your list")
11
print(movies)
12
13
main()
14