How to input multiple values from user in one ... - Tutorialspoint

文章推薦指數: 80 %
投票人數:10人

In Python, to get values from users, use the input(). This works the same as scanf() in C language. Input multiple values from a user in a single line using ... Home CodingGround Jobs Whiteboard Tools Business Teachwithus TrendingCategories DataStructure Networking RDBMS OperatingSystem Java iOS HTML CSS Android Python CProgramming C++ C# MongoDB MySQL Javascript PHP SelectedReading UPSCIASExamsNotes Developer'sBestPractices QuestionsandAnswers EffectiveResumeWriting HRInterviewQuestions ComputerGlossary WhoisWho HowtoinputmultiplevaluesfromuserinonelineinPython? ProgrammingPythonServerSideProgramming ProgrammingMethodologiesTraining 31Lectures 1.5hours Shweta MoreDetail Linearprogramming 15Lectures 1hours RidhiArora MoreDetail PythonProgramming 26Lectures 7.5hours DATAhillSolutionsSrinivasReddy MoreDetail InPython,togetvaluesfromusers,usetheinput().Thisworksthesameasscanf()inClanguage. Inputmultiplevaluesfromauserinasinglelineusinginput() Toinputmultiplevaluesinoneline,usetheinput()method− x,y,z=input(),input(),input() Let’ssaytheuserentered5,10,15.Now,youcandisplaythemonebyone− print(x) print(y) print(z) Output 5 10 15 Fromaboveoutput,youcansee,weareabletogivevaluestothreevariablesinoneline.Toavoidusingmultipleinput()methods(dependsonhowmanyvalueswearepassing),wecanusethelistcomprehensionormap()function. Inputmultiplevaluesfromauserinasinglelineusinglist comprehension WehaveusedtheListComprehensionwiththeinput()methodtoinputmultiplevaluesinasingleline− a,b,c=[int(a)foraininput().split()] Let’ssaytheuserentered1,2,13Now,youcandisplaythemonebyone− print(x) print(y) print(z) Output 1 2 3 Inputmultiplevaluesfromauserinasinglelinelineusingmap() Themap()methodcanalsobeusedinPythontoinputmultiplevaluesfromauserinasingleline− a,b,c=map(int,input().split()) Let’ssaytheuserentered1,2,13.Now,youcandisplaythemonebyone− print(x) print(y) print(z) Output 1 2 3 AmitDiwan Updatedon12-Aug-202212:44:02 RelatedQuestions&AnswersHowtoinputmultiplevaluesfromuserinonelineinC#? HowtoinputmultiplevaluesfromuserinonelineinJava? TakeMatrixinputfromuserinPython HowtodisplaymultiplelabelsinonelinewithPythonTkinter? TakingmultipleinputsfromuserinPython Howtocatchmultipleexceptionsinoneline(exceptblock)inPython? HowtoconcatenatemultipleC++stringsononeline? PythonGetalistasinputfromuser TakinginputfromtheuserinTkinter IterateoverlinesfrommultipleinputstreamsinPython HowtopopulateanarrayonevalueatatimebytakinginputfromuserinJava? HowtocreateinputPop-Ups(Dialog)andgetinputfromuserinJava? HowtoexecutePythonmulti-linestatementsintheone-lineatcommand-line? JavaProgramtoGetInputfromtheUser HowtoCreateMultipleUserAccountsinLinux? PreviousPage PrintPage NextPage  Advertisements



請為這篇文章評分?