什么都不赋值是编程不好的标志吗?

Is assignment to nothing a sign of bad programming?

我写了一个运行完美的程序,除了我得到的反馈说 "you assigned the expression to nothing"。这是代码:

l = input('Please insert the numbers: ')

numbers = [int(i) for i in l.split(',')]

lower_limit = int(input('Please insert the lower limit: '))
upper_limit = int(input('Please insert the uppper limit: '))

boolean_list = []

for i in numbers:
       boolean_list.append(True) if (i >= lower_limit and i <= upper_limit ) else  
       boolean_list.append(False)    

print(boolean_list)

我可以先初始化 boolean_list 然后给它赋逻辑值,但我相信它会比我的第一种方法(上面给出的)慢得多。我对编程完全陌生,所以我不清楚我做错了什么。

避免此问题的最佳选择是通过使用列表理解来摆脱 for 循环:

boolean_list = [i >= lower_limit and i <= upper_limit  for i in numbers]

这将使用 list comprehensions 构建迭代器的更有效方法。