在lis中中断用户输入

2024-09-30 10:33:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我无法使用用户输入从扩展列表中跳出。我想我缺少了如何使用if语句来查询特定项的列表。当用户输入-999时,我需要一个列表来请求输入。我还需要从列表中排除-999。你能帮助我吗?你知道吗

print(scoreLst)就是为了在我使用它时可以测试和查看它是如何工作的。你知道吗

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

Tags: the用户testtrue列表inputif语句
1条回答
网友
1楼 · 发布于 2024-09-30 10:33:07

注意事项:

  • 将考试成绩转换为int

  • list.append返回None,不要将它赋给任何对象;使用scoreLst[-1]而不是score1

  • 不要使用list.remove删除列表的最后一个元素,list.pop()将很好地工作

  • lst1 == False是比较,lst1 = False是赋值

  • 您将创建一个无限循环,break一旦用户输入-999,我认为没有必要lst1

最终结果:

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

相关问题 更多 >

    热门问题