Reputation: 313
I want it to ask the user to input their favorite film and it to add that input at the end of the list and then display the list again. as well as making sure the film entered is not in the least already. this is how far ive got up to :
films = ["Star Wars", "Lord of the Rings", "Shawshank Redemption", "God father"]
for films in films:
print ("%s" % films)
add = input("Please enter a film: ")
films.insert(-1,add)
print (films)
Edit:
When I tried that I get an error:
Traceback (most recent call last):
File "H:\Modules (Year 2)\Advanced programming\Python\Week 2 - Review and Arrays\Week 2.2 - Exercises.py", line 48, in films.append(add) AttributeError: 'str' object has no attribute 'append'
Upvotes: 0
Views: 21788
Reputation: 1
"""
This is for a dictionary
""""
Cities={"London":0,"Cairo":2182,"Lagos":3110,"Lima":6314}
while True:
city=input("Enter a city")
if city in Cities:
print (Cities[city])
else:
print("Not in dictionary please input the distance")
distance=input()
Cities[city]=int(distance)
print (Cities)
Upvotes: 0
Reputation: 60137
The error is here:
for films in films:
print("%s" % films)
You are doing for films in films
instead of for film in films
.
Also, the others are correct that a better way of writing films.insert(-1, ...)
is films.append(...)
.
Upvotes: 1
Reputation: 889
Check if it is already in list with not in
and use append
to add it to the end of the list:
films = ["Star Wars", "Lord of the Rings", "Shawshank Redemption", "God father"]
for films in films:
print ("%s" % films)
add = input("Please enter a film: ")
if add not in films: films.append(add)
else: print("Already in list!")
print (films)
Upvotes: 0
Reputation: 250951
Use in
to check whether the item is already present in list or not and then use list.append
to add that item to the end of the list.
add = input("Please enter a film: ")
if add not in films:
films.append(add)
Note that in the for-loop you've replaced films
with a string, use some other variable name:
for film in films:
print ("%s" % film)
Upvotes: 3