简体   繁体   中英

How to take multiple inputs from a user in Python

I'm trying to create a program where python will take multiple inputs until the user enters "END". Each input must be on a different line. I'm still new and learning python/programming in general so is there any way to do this? and how would I turn the inputs into a list?

What a demo might look like -

What are your hobbies?

painting

hiking

drawing

END

You can read input with the input() function. You want to read till 'END' is entered by the user. So, read the input and check whether it is 'END'. If not append it to the list else exit the loop.

In codes you can do like this.

inputs = [] # list to  store the inputs

while True: # looping forever
    
    data = input('Enter the data : ') # read the data from user to variable data
    
    if data == 'END': # if END is read then exit the loop
        break
    
    else:
        inputs.append(data) # otherwise, append the input to the list
        
print(inputs) # display the list.

Hope this helps.!!

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