How to store user input into list on Python - Stack Overflow
文章推薦指數: 80 %
This is my way of storing input into a list: list = [] i = 1 while i < 6: a = input("Please enter " + str(i) + " list : ") list.append(a) ... Home Public Questions Tags Users Companies Collectives ExploreCollectives Teams StackOverflowforTeams –Startcollaboratingandsharingorganizationalknowledge. CreateafreeTeam WhyTeams? Teams CreatefreeTeam Collectives™onStackOverflow Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost. LearnmoreaboutCollectives Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. LearnmoreaboutTeams HowtostoreuserinputintolistonPython AskQuestion Asked 6years,3monthsago Modified 2years,7monthsago Viewed 36ktimes 0 I'mhavingabitoftroubleputtinguserinputsintoalist. Ibasicallywanttheusertoinputabout5thingandhaveeachitemstoredinthelistindividually.Ithenwanttodisplayalltheinputinsaidlist.Ifanyonecangiveanybitofguidanceitwillbegreatlyappreciated. Thisiswhatihavesofar: mylist=[1,2,3,4,5] printmylist print"Enter5itemsonshoppinglist" foriinmylist: shopping=raw_input() printshopping pythonlistloopsfor-loop Share Follow editedMay1,2016at2:44 Bahrom 4,5822929silverbadges4040bronzebadges askedMay1,2016at2:41 SuzzySuzzy 3322goldbadges22silverbadges77bronzebadges 2 1 Haveyoulookedatthepythondocs?docs.python.org/2/tutorial/datastructures.html#more-on-lists – Bahrom May1,2016at2:44 thanksIamusingthelink – Suzzy May1,2016at3:04 Addacomment | 2Answers 2 Sortedby: Resettodefault Highestscore(default) Trending(recentvotescountmore) Datemodified(newestfirst) Datecreated(oldestfirst) 4 IstronglyurgeyoutoreadthroughthePythondocswhichprovideyouwithsomebasicexamplesoflistoperation-openupashell,andtypethoseexamplesoutforyourself.Basicallyyouwanttostoresomeone'sinputintomylist,sothereisnoneedtopredefineitwithvalues: mylist=[] Nowyouwanttopromptauser5times(toenter5items): print"Enter5itemsonshoppinglist" foriinxrange(5):#rangestartsfrom0andendsat5-1(so0,1,2,3,4executesyourloopcontents5times) shopping=raw_input() mylist.append(shopping)#addinputtothelist printmylist#atthispointyourlistcontainsthe5thingsenteredbyuser Share Follow editedMay1,2016at2:58 answeredMay1,2016at2:46 BahromBahrom 4,5822929silverbadges4040bronzebadges 2 xrange(5)orrange(5).Itstartsat0butitendsat4. – KlausD. May1,2016at2:54 @KlausD.you'reright,sorryI'msomewhatslowtonight. – Bahrom May1,2016at3:01 Addacomment | 2 Thisismywayofstoringinputintoalist: list=[] i=1 whilei<6: a=input("Pleaseenter"+str(i)+"list:") list.append(a) i+=1 print(list) Share Follow editedJan5,2020at2:40 answeredJan5,2020at2:33 jadjad93hehejadjad93hehe 2122bronzebadges 1 1 whywouldyouusewhilewithi+=1insteadofforandrange? – njzk2 Jan5,2020at2:33 Addacomment | YourAnswer ThanksforcontributingananswertoStackOverflow!Pleasebesuretoanswerthequestion.Providedetailsandshareyourresearch!Butavoid…Askingforhelp,clarification,orrespondingtootheranswers.Makingstatementsbasedonopinion;backthemupwithreferencesorpersonalexperience.Tolearnmore,seeourtipsonwritinggreatanswers. Draftsaved Draftdiscarded Signuporlogin SignupusingGoogle SignupusingFacebook SignupusingEmailandPassword Submit Postasaguest Name Email Required,butnevershown PostYourAnswer Discard Byclicking“PostYourAnswer”,youagreetoourtermsofservice,privacypolicyandcookiepolicy Nottheansweryou'relookingfor?Browseotherquestionstaggedpythonlistloopsfor-looporaskyourownquestion. TheOverflowBlog Opensourceandaccidentalinnovation TheluckiestguyinAI(Ep.477) FeaturedonMeta Recentsiteinstability,majoroutages–July/August2022 PleasewelcomeValuedAssociate#1301-Emerson StagingGroundWorkflow:QuestionLifecycle Related 3231 HowdoIcheckifalistisempty? 2223 HowcanIrandomlyselectanitemfromalist? 2899 HowdoIsplitalistintoequally-sizedchunks? 3581 HowdoIgetthecurrenttime? 2614 HowdoIgetthelastelementofalist? 4780 HowdoImakeaflatlistoutofalistoflists? 2193 HowdoIgetthenumberofelementsinalistinPython? 3246 HowdoIconcatenatetwolistsinPython? 3114 HowdoIclonealistsothatitdoesn'tchangeunexpectedlyafterassignment? 3467 HowdoIlistallfilesofadirectory? HotNetworkQuestions Algorithmwhichfindsthecountofvaluesaresmallertotherightthanthecurrentone(inthelist) HowdoIgetroundededgesonafootballshapeinInkscape? HowtodisableGNOMEScreenshotUI,selectionframe,necessitytoclicksomethingtomakeascreenshotofanareainUbuntu22.04? Removingmissingdatafromalist Partitionofthelonglist DoescoveringanobjectwithDarknesscastuponitcountasusinganobjectasdescribedinthecombatactions? Whatportstoopenforsqlserverbehindsecuredomainfirewall Priortothestudentloanrepaymentchanges--whencouldanindividualwindupbeingchargedmoreininterestthantheirminimumpayments? ImprovedSieveofEratosthenes WhatisthepointofATMtransactionlimits? Whatisthesignificanceofreachingmajorversion1.0.0? musig2withBIP-32stylenoncechain Ifvampireswerereal,whatwouldonewhobrokeintosomeone'shometodrinktheirbloodbechargedwith? WouldaRussianvisabanbecompliantwiththeUniversalDeclarationofHumanRights? Canaphotonorbitaroundagalaxy? Galatians4:14.PaulsaysthatJesusisAngeloftheLord? 25PINsound/gameribboncable WhyweretheWestAfricanislandsuninhabited? Howmanyspacerswerethere? Howdoesonemakesurethatthere[hkl]directioninab-initocalculationsisthesameasinexperimentalcalculations? Whydoes'onsteroids'suggestthemeaningofexaggerationorenhancement? StartabatfileonremotePC HowcanImonitormyrooftodetectroofleaksbeforetheycausedamage? Wherecouldweaddanewplanettotheinnersolarsystem? morehotquestions Questionfeed SubscribetoRSS Questionfeed TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader. lang-py Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings
延伸文章資訊
- 1User Input For List | Python Programs - YouTube
- 2How to store user input into list on Python - Stack Overflow
This is my way of storing input into a list: list = [] i = 1 while i < 6: a = input("Please enter...
- 3How 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...
- 4Python Accept List as a input From User - PYnative
- 5Python Get a list as input from user - Tutorialspoint
We can also use input function twice so that we can create a list of lists. Use the range functio...