简体   繁体   中英

Breaking out of user inputs in a list

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

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM