Python code:
PeoplesNames = []
while len(PeoplesNames) < 3:
person = input('Enter your name: ')
PeoplesNames.append(person)
print PeoplesNames
if 'Dan' in PeoplesNames:
PeoplesNames.pop('Dan')
print PeoplesNames
From my understanding this should run through the while loop (which it does) until the list hits length of 3 (which it does) then print the list (which it does) then hit the if statement and delete dan from the list and then print the new list (which it does not) do i need to nest the if statement or something else? thanks
list.pop() #used to pop out the top of the stack(list) or
list.pop(index) #can be an index that needed to be pop out, but
list.remove(item) # removes the item specified
Try with the below solution
if 'Dan' in PeoplesNames:
PeoplesNames.remove('Dan')
print PeoplesNames
or - keeping EAFP in mind - you could:
PeoplesNames = [];
while len(PeoplesNames) < 3:
person = raw_input('Enter your name: ')
PeoplesNames.append(person)
print PeoplesNames
try:
PeoplesNames.remove('Dan')
except ValueError:
pass
print PeoplesNames
also note that in python 2.7
you need to use raw_input()
instead of input()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.