Search code examples
pythonpython-3.xlist-comprehensionuser-input

How do you ask for user input in list comprehension?


I'm trying to figure out how to ask for a user's input in list comprehension format.

SR = [srt for i in range (N) input("Enter a type of food")]
print (SR)

Assuming N was 2 for example, I would like to get a list that contains 2 user inputs essentially. However the code about doesn't work.


Solution

  • You have the syntax wrong. This is how you do it

    SR = [input("Enter a type of food: ") for _ in range (N)]