How to take integer input in Python - Java2Blog

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

Python 3.x example ; a = int · input("Enter an Integer: ") ; b = int · input("Enter an Integer: ") ; print · "Sum of a and b:" · a + b ... LearnJavakeyboard_arrow_left PreviousNext keyboard_arrow_right searchreportthisadRelatedPostsGetDayNamefromDateinPythonFillArraywithRandomNumbersinPythonConvertRomanNumbertoIntegerinPythonRemoveSingleQuotesfromStringinPythonEscapeBackslashCharacterinPythonBreakoutoffunctioninPythonCheckifObjectIsIterableinPythonSaveNumpyArrayasImageinPythonReplaceTabswithSpacesinPythonCountDowninaforLoopinPythonreportthisadHome>Python>HowtotakeintegerinputinPythonTableofContentsPython3.xexamplePython2.xexampleWasthisposthelpful?Inthistutorial,wewillseehowtotakeintegerinputinPythonThereisdifferenceinhowyoucantakeinputinpython2.xand3.x.Youneedtouseraw_inputinpython2.xandinputinPython3.xInPython3.x,raw_inputwasrenamedtoinputandthePython2.xinputwasremoved.Asyoumightknow,raw_inputalwaysreturnsaStringobjectandsameisthecasewithinputinPython3.xTofixtheproblem,youneedtoexplicitlymakethoseinputsintointegersbyputtingtheminint()function.Python3.xexamplea=int(input(“Enteranumber:“))b=int(input(“Enteranumber:“))Let’sunderstandwiththehelpofexample.123456 a=int(input("EnteranInteger:"))b=int(input("EnteranInteger:"))print("Sumofaandb:",a+b)print("Multiplicationofaandb:",a*b) Output:EnteranInteger:20EnteranInteger:10Sumofaandb:30Multiplicationofaandb:200Python2.xexamplep=int(raw_input(“Enteranumber:“))q=int(raw_input(“Enteranumber:“))Let’sunderstandwiththehelpofexample.123456 p=int(raw_input("EnteranInteger:"))q=int(raw_input("EnteranInteger:"))print("Sumofpandq:",p+q)print("Multiplicationofpandq:",p*q) Output:EnteranInteger:20EnteranInteger:10Sumofaandb:30Multiplicationofaandb:200That’sallaboutHowtotakeintegerinputinPython.Wasthisposthelpful?Letusknowifyoulikedthepost.That’stheonlywaywecanimprove. Yes Noimport_contactsYoumayalsolike:GetDayNamefromDateinPythonFillArraywithRandomNumbersinPythonConvertRomanNumbertoIntegerinPythonRemoveSingleQuotesfromStringinPythonEscapeBackslashCharacterinPythonBreakoutoffunctioninPythonCheckifObjectIsIterableinPythonSaveNumpyArrayasImageinPythonReplaceTabswithSpacesinPythonCountDowninaforLoopinPython123…30›SharethisPrevCombinetwolistsinPythonNextPythoncreateemptysetreportthisadAuthorArpitMandliyaFollowAuthorRelatedPostsPythonPythonDate29June GetDayNamefromDateinPythonTableofContentsUsetheCalendarModuletoGettheNameoftheDayinPythonUsethestrftime()MethodtoGettheNameoftheDayinPythonUsePandasTimestampMethodtoGettheNameoftheDayinPythonUsetheweekday()MethodtoGettheNameoftheDayinPythonUsetheisoweekday()Methodto[…]ReadMore PythonPythonArray27June FillArraywithRandomNumbersinPythonTableofContentsFillarraywithrandomnumbersinPythonWaystofillarraywithrandomnumbersinPythonUsingthenumpy.random.randint()functiontofillarraywithrandomnumbersinPythonUsingthenumpy.random.Generator.integers()functiontofillarraywithrandomnumbersinPythonUsingtherandom.randint()functiontofillarraywithrandomnumbersinPythonConclusionWasthisposthelpful?Fillarray[…]ReadMore PythonPythonmaths25June ConvertRomanNumbertoIntegerinPythonTableofContentsHowtoConvertRomanNumbertoIntegerinPythonHowtoconvertromannumbertointegerinpython?Usingtheif...elsestatementtoconvertromannumbertointegerinPython.UsingclassesandadictionarytoconvertromannumbertointegerinPython.UsingtheromanmoduletoconvertromannumbertointegerinPython.ConclusionWasthispost[…]ReadMore Python11June RemoveSingleQuotesfromStringinPythonTableofContentsRemovesinglequotesfromstringinPython.HowtoremovesinglequotesfromstringinPython?Usingthereplace()functiontoremovesinglequotesfromstringinPython.UsingaforlooptoremovesinglequotesfromstringinPython.Usingthejoin()functiontoremovesinglequotesfromstringinPython.Usingtheremodulefunctionsto[…]ReadMore Python11June EscapeBackslashCharacterinPythonTableofContentsEscapebackslashcharacterinpython.Howtouseescapebackslashcharacterinpython?Usingtheescapebackslashcharacterinpythontorepresentwhitespacecharacters.Usingtheescapebackslashcharacterinpythontoturnspecialcharactersintoordinarycharacters.Usingtheescapebackslashcharacterinf-stringsinPython.UsingtheescapebackslashcharacterinrawstringsinPython.Wasthis[…]ReadMore Python11June BreakoutoffunctioninPythonTableofContentsBreakoutoffunctioninPythonHowtobreakoutoffunctioninPython?UsingthereturnstatementtobreakoutoffunctioninPythonUsingthetryandexceptblocktobreakoutoffunctioninPythonUsingthesys.exit()functiontobreakoutoffunctioninPythonConclusionWasthisposthelpful?Inthispost,wewill[…]ReadMore LeaveaReplyCancelreplyYouremailaddresswillnotbepublished.Requiredfieldsaremarked*Comment*Name*Email*WebsiteSavemyname,email,andwebsiteinthisbrowserforthenexttimeIcomment. ΔSubscribetoournewletterGetqualitytutorialstoyourinbox.Subscribenow.Let’sbeFriendsFacebookTwitterLinkedIn©2020Java2Blogxx



請為這篇文章評分?