Reputation: 55
I'm having trouble breaking out of my expanded list using a user input. I think I am missing how to use an if
statement to query a list for a specific item. I need the list to break out asking for inputs when the user enters -999. I also need to exclude -999 from the list. Can you help me?
The print(scoreLst)
is just so can test and see how it is working as I use it.
scoreLst =[]
score = ()
lst1 = True
print("The list ends when user inputs -999")
scoreLst.append(input("Enter the test score: "))
while lst1 == True:
score1 = scoreLst.append(input("Enter another test score: "))
print(scoreLst)
if score1 != -999:
lst1 == True
else:
scoreLst.remove(-999)
lst1 == False
Upvotes: 1
Views: 395
Reputation: 46523
A few notes:
convert test scores to int
list.append
returns None
, don't assign it to anything; use scoreLst[-1]
instead of score1
don't use list.remove
to delete the last element of the list, list.pop()
will work nicely
lst1 == False
is comparison, lst1 = False
is assignment
you'd create an infinite loop and break
once the user enters -999, I see no need for lst1
End result:
scoreLst = []
print("The list ends when user inputs -999")
scoreLst.append(int(input("Enter the test score: ")))
while True:
scoreLst.append(int(input("Enter another test score: ")))
if scoreLst[-1] == -999:
scoreLst.pop()
break
Upvotes: 2