简体   繁体   中英

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.

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

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

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