0

I am trying to make a plan that allows you to insert strings to a list until you type a certain keyword which then locks the list from further appending.

print("I will list everything you desire!")
list = []
while input("") != "stop":
    shop_list = [list.append(i) for i in input("")]
print(shop_list)
list.clear()

Yet once I run this program, I get an output that has a few issues. output: [None,None] (amount of None is per the number of inputs you give)

In addition to this, after the program finishes running I don't seem to get list cleared. I don't understand why, considering that the clear function should do just as I intend.

Regarding the first problem, I assume it is due to incorrect use of input("") in the extent of list comprehension.

And with the latter issue, I suppose I might have used clear function incorrectly. Although this is how I should use it.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 2
    `list.append` add to the list AND returns none, so you're collecting None values in the list comprhension, that's normal – azro Dec 31 '22 at 16:19
  • so how can I do so otherwise? I still desire to finish my plan – stuck overflawed Dec 31 '22 at 16:23
  • 2
    Don't overshadow the built-in `list` as your variable name. – Daniel Hao Dec 31 '22 at 16:49
  • 1
    I doubt if *for i in input("")* is doing what you think it's doing. Try it in isolation - i.e., outside of the list comprehension – DarkKnight Dec 31 '22 at 17:39
  • What makes you say `list` is not being cleared? If you just add `print(list)` at the end, you get `[]`. – wjandrea Dec 31 '22 at 17:51
  • More info on what Daniel said: `list` is a bad variable name since it [shadows](https://en.wikipedia.org/wiki/Variable_shadowing) the [builtin `list` type](https://docs.python.org/3/library/stdtypes.html#list). It's better to use a more descriptive name like `inputs`, or at least something like `lst`. Cf. [TypeError: 'list' object is not callable](/q/31087111/4518341) – wjandrea Dec 31 '22 at 17:52
  • Please take the [tour]. Stack Overflow is for specific questions, but this isn't a question and you're describing two separate issues. See [ask]. For help with debugging, check out [How to step through Python code to help debug issues?](/q/4929251/4518341) and [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) by Eric Lippert. – wjandrea Dec 31 '22 at 17:59

1 Answers1

0

You prob. can try this, as earlier comment pointed out your syntax is off... so it needs to be rewrite and corrected:

Note - in Python 3.8+you can change this line - while (item := input()) != 'stop':


print("I will list everything you desire!")
# L = []
shop_list = []

while True:                    # Loop continuously
    item = input()             # get the input - one item at a time (one line) 
    if item != 'stop':         #  not done yet....
        shop_list.append(item)
    else:
        break
    
print(f' the shop list: {shop_list}')

shop_list.clear()

# Outputs Example:  assuming typing each word by itself (one line each word)
# the shop list: ['apple ', 'orange', 'banana ']
Daniel Hao
  • 4,922
  • 3
  • 10
  • 23