Add user input to a list in Python - bobbyhadz
文章推薦指數: 80 %
Declare a variable that stores an empty list. Use a range to iterate N times. On each iteration, prompt the user for input. append the value to ...
☰HomeBookAboutContactsHomeBookAboutContactsGitHubLinkedinTwitterAdduserinputtoalistinPythonBorislavHadzhievLastupdated:Jun21,2022PhotofromUnsplashAdduserinputtoalistinPython#ToadduserinputtoalistinPython:Declareavariablethatstoresanemptylist.UsearangetoiterateNtimes.Oneachiteration,prompttheuserforinput.appendthevaluetothelist.main.pyCopied!shopping_list=[]
list_length=3
foridxinrange(list_length):
item=input('Enteritemtobuy:')
shopping_list.append(item)
print(shopping_list)#👉️['apple','banana','kiwi']
Thecodesamplepromptstheuserforinput3timesandaddseachvaluetothe
list.The
list.append()
methodaddsanitemtotheendofthelist.Theexampleusesthe
rangeclass,butyou
canalsouseawhileloopifyouwanttomakesurethelisthasalengthofat
leastNitems.main.pyCopied!shopping_list=[]
max_length=3
whilelen(shopping_list)
延伸文章資訊
- 1Python | Get a list as input from user - GeeksforGeeks
In this article, we will see how to get as input a list from the user. Examples: Input : n = 4, e...
- 2User Input For List | Python Programs - YouTube
- 3user input for item in list python Code Example
n = int(input("Enter number of elements : ")). 3. 4. # Below line read inputs from user using map...
- 4Python Accept List as a input From User - PYnative
- 5How To Input A List In Python - Edureka
This article will introduce you to different ways to input a list in Python and give you a detail...