[{"data":1,"prerenderedAt":1802},["ShallowReactive",2],{"doc-\u002Freference\u002Fpython-input-function-explained":3},{"id":4,"title":5,"body":6,"description":1795,"extension":1796,"meta":1797,"navigation":807,"path":1798,"seo":1799,"stem":1800,"__hash__":1801},"content\u002Freference\u002Fpython-input-function-explained.md","Python input() Function Explained",{"type":7,"value":8,"toc":1761},"minimark",[9,19,26,29,34,101,107,110,117,122,125,143,146,192,195,226,229,245,247,251,254,271,274,287,290,326,329,357,363,365,371,376,379,381,435,437,480,487,505,507,559,561,593,595,599,604,647,654,660,714,723,730,734,830,832,867,870,872,932,934,960,970,972,976,981,988,991,1029,1032,1051,1054,1058,1061,1120,1138,1148,1150,1177,1186,1193,1197,1200,1238,1241,1247,1286,1288,1292,1298,1317,1322,1371,1375,1471,1474,1481,1483,1487,1492,1510,1513,1556,1562,1582,1584,1646,1648,1652,1659,1668,1674,1680,1690,1702,1706,1714,1716,1720,1757],[10,11,13,14,18],"h1",{"id":12},"python-input-function-explained","Python ",[15,16,17],"code",{},"input()"," Function Explained",[20,21,22,23,25],"p",{},"The ",[15,24,17],{}," function lets your Python program receive text from the user.",[20,27,28],{},"It is one of the most common beginner tools in Python because it makes programs interactive. You can use it to ask for a name, an age, a number, or any other value the user types.",[30,31,33],"h2",{"id":32},"quick-example","Quick example",[35,36,41],"pre",{"className":37,"code":38,"language":39,"meta":40,"style":40},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","name = input(\"Enter your name: \")\nprint(\"Hello, \" + name)\n","python","",[15,42,43,77],{"__ignoreMap":40},[44,45,48,52,56,60,64,68,72,74],"span",{"class":46,"line":47},"line",1,[44,49,51],{"class":50},"su5hD","name ",[44,53,55],{"class":54},"smGrS","=",[44,57,59],{"class":58},"sptTA"," input",[44,61,63],{"class":62},"sP7_E","(",[44,65,67],{"class":66},"sjJ54","\"",[44,69,71],{"class":70},"s_sjI","Enter your name: ",[44,73,67],{"class":66},[44,75,76],{"class":62},")\n",[44,78,80,83,85,87,90,92,95,99],{"class":46,"line":79},2,[44,81,82],{"class":58},"print",[44,84,63],{"class":62},[44,86,67],{"class":66},[44,88,89],{"class":70},"Hello, ",[44,91,67],{"class":66},[44,93,94],{"class":54}," +",[44,96,98],{"class":97},"slqww"," name",[44,100,76],{"class":62},[20,102,103,104,106],{},"Use ",[15,105,17],{}," to get text from the user. It always returns a string.",[108,109],"hr",{},[30,111,113,114,116],{"id":112},"what-input-does","What ",[15,115,17],{}," does",[20,118,119,121],{},[15,120,17],{}," pauses the program and waits for the user to type something.",[20,123,124],{},"Here is what happens:",[126,127,128,134,137,140],"ul",{},[129,130,131,133],"li",{},[15,132,17],{}," waits for the user to type",[129,135,136],{},"It reads text from the keyboard",[129,138,139],{},"It returns the typed value as a string",[129,141,142],{},"The program continues after the user presses Enter",[20,144,145],{},"Example:",[35,147,149],{"className":37,"code":148,"language":39,"meta":40,"style":40},"color = input(\"What is your favorite color? \")\nprint(\"Your favorite color is\", color)\n",[15,150,151,171],{"__ignoreMap":40},[44,152,153,156,158,160,162,164,167,169],{"class":46,"line":47},[44,154,155],{"class":50},"color ",[44,157,55],{"class":54},[44,159,59],{"class":58},[44,161,63],{"class":62},[44,163,67],{"class":66},[44,165,166],{"class":70},"What is your favorite color? ",[44,168,67],{"class":66},[44,170,76],{"class":62},[44,172,173,175,177,179,182,184,187,190],{"class":46,"line":79},[44,174,82],{"class":58},[44,176,63],{"class":62},[44,178,67],{"class":66},[44,180,181],{"class":70},"Your favorite color is",[44,183,67],{"class":66},[44,185,186],{"class":62},",",[44,188,189],{"class":97}," color",[44,191,76],{"class":62},[20,193,194],{},"Possible output:",[35,196,198],{"className":37,"code":197,"language":39,"meta":40,"style":40},"What is your favorite color? blue\nYour favorite color is blue\n",[15,199,200,217],{"__ignoreMap":40},[44,201,202,204,207,210,214],{"class":46,"line":47},[44,203,113],{"class":50},[44,205,206],{"class":54},"is",[44,208,209],{"class":50}," your favorite color",[44,211,213],{"class":212},"srjyR","?",[44,215,216],{"class":50}," blue\n",[44,218,219,222,224],{"class":46,"line":79},[44,220,221],{"class":50},"Your favorite color ",[44,223,206],{"class":54},[44,225,216],{"class":50},[20,227,228],{},"In this example:",[126,230,231,236,242],{},[129,232,233,234],{},"The prompt is ",[15,235,166],{},[129,237,238,239],{},"The user's answer is stored in ",[15,240,241],{},"color",[129,243,244],{},"That value is then printed",[108,246],{},[30,248,250],{"id":249},"basic-syntax","Basic syntax",[20,252,253],{},"The basic syntax is:",[35,255,257],{"className":37,"code":256,"language":39,"meta":40,"style":40},"input(prompt)\n",[15,258,259],{"__ignoreMap":40},[44,260,261,264,266,269],{"class":46,"line":47},[44,262,263],{"class":58},"input",[44,265,63],{"class":62},[44,267,268],{"class":97},"prompt",[44,270,76],{"class":62},[20,272,273],{},"Important points:",[126,275,276,281,284],{},[129,277,22,278,280],{},[15,279,268],{}," is optional",[129,282,283],{},"The prompt appears before the user types",[129,285,286],{},"You usually store the result in a variable",[20,288,289],{},"Example with a prompt:",[35,291,293],{"className":37,"code":292,"language":39,"meta":40,"style":40},"username = input(\"Enter your username: \")\nprint(username)\n",[15,294,295,315],{"__ignoreMap":40},[44,296,297,300,302,304,306,308,311,313],{"class":46,"line":47},[44,298,299],{"class":50},"username ",[44,301,55],{"class":54},[44,303,59],{"class":58},[44,305,63],{"class":62},[44,307,67],{"class":66},[44,309,310],{"class":70},"Enter your username: ",[44,312,67],{"class":66},[44,314,76],{"class":62},[44,316,317,319,321,324],{"class":46,"line":79},[44,318,82],{"class":58},[44,320,63],{"class":62},[44,322,323],{"class":97},"username",[44,325,76],{"class":62},[20,327,328],{},"Example without a prompt:",[35,330,332],{"className":37,"code":331,"language":39,"meta":40,"style":40},"value = input()\nprint(value)\n",[15,333,334,346],{"__ignoreMap":40},[44,335,336,339,341,343],{"class":46,"line":47},[44,337,338],{"class":50},"value ",[44,340,55],{"class":54},[44,342,59],{"class":58},[44,344,345],{"class":62},"()\n",[44,347,348,350,352,355],{"class":46,"line":79},[44,349,82],{"class":58},[44,351,63],{"class":62},[44,353,354],{"class":97},"value",[44,356,76],{"class":62},[20,358,359,360,362],{},"You can use ",[15,361,17],{}," without a prompt, but a clear prompt is usually better. It helps the user know what to enter.",[108,364],{},[30,366,113,368,370],{"id":367},"what-input-returns",[15,369,17],{}," returns",[20,372,373,375],{},[15,374,17],{}," always returns a string.",[20,377,378],{},"That is true even if the user types a number.",[20,380,145],{},[35,382,384],{"className":37,"code":383,"language":39,"meta":40,"style":40},"age = input(\"Enter your age: \")\nprint(age)\nprint(type(age))\n",[15,385,386,406,417],{"__ignoreMap":40},[44,387,388,391,393,395,397,399,402,404],{"class":46,"line":47},[44,389,390],{"class":50},"age ",[44,392,55],{"class":54},[44,394,59],{"class":58},[44,396,63],{"class":62},[44,398,67],{"class":66},[44,400,401],{"class":70},"Enter your age: ",[44,403,67],{"class":66},[44,405,76],{"class":62},[44,407,408,410,412,415],{"class":46,"line":79},[44,409,82],{"class":58},[44,411,63],{"class":62},[44,413,414],{"class":97},"age",[44,416,76],{"class":62},[44,418,420,422,424,428,430,432],{"class":46,"line":419},3,[44,421,82],{"class":58},[44,423,63],{"class":62},[44,425,427],{"class":426},"sZMiF","type",[44,429,63],{"class":62},[44,431,414],{"class":97},[44,433,434],{"class":62},"))\n",[20,436,194],{},[35,438,440],{"className":37,"code":439,"language":39,"meta":40,"style":40},"Enter your age: 25\n25\n\u003Cclass 'str'>\n",[15,441,442,454,459],{"__ignoreMap":40},[44,443,444,447,450],{"class":46,"line":47},[44,445,446],{"class":50},"Enter your age",[44,448,449],{"class":62},":",[44,451,453],{"class":452},"srdBf"," 25\n",[44,455,456],{"class":46,"line":79},[44,457,458],{"class":452},"25\n",[44,460,461,464,468,471,474,477],{"class":46,"line":419},[44,462,463],{"class":54},"\u003C",[44,465,467],{"class":466},"sbsja","class",[44,469,470],{"class":66}," '",[44,472,473],{"class":70},"str",[44,475,476],{"class":66},"'",[44,478,479],{"class":54},">\n",[20,481,482,483,486],{},"Even though the user typed ",[15,484,485],{},"25",", Python treats it as text.",[20,488,489,490,497,498,504],{},"If you need a number, convert it with ",[491,492,494],"a",{"href":493},"\u002Freference\u002Fpython-int-function-explained\u002F",[15,495,496],{},"int()"," or ",[491,499,501],{"href":500},"\u002Freference\u002Fpython-float-function-explained\u002F",[15,502,503],{},"float()",".",[20,506,145],{},[35,508,510],{"className":37,"code":509,"language":39,"meta":40,"style":40},"age = int(input(\"Enter your age: \"))\nprint(age)\nprint(type(age))\n",[15,511,512,535,545],{"__ignoreMap":40},[44,513,514,516,518,521,523,525,527,529,531,533],{"class":46,"line":47},[44,515,390],{"class":50},[44,517,55],{"class":54},[44,519,520],{"class":426}," int",[44,522,63],{"class":62},[44,524,263],{"class":58},[44,526,63],{"class":62},[44,528,67],{"class":66},[44,530,401],{"class":70},[44,532,67],{"class":66},[44,534,434],{"class":62},[44,536,537,539,541,543],{"class":46,"line":79},[44,538,82],{"class":58},[44,540,63],{"class":62},[44,542,414],{"class":97},[44,544,76],{"class":62},[44,546,547,549,551,553,555,557],{"class":46,"line":419},[44,548,82],{"class":58},[44,550,63],{"class":62},[44,552,427],{"class":426},[44,554,63],{"class":62},[44,556,414],{"class":97},[44,558,434],{"class":62},[20,560,194],{},[35,562,564],{"className":37,"code":563,"language":39,"meta":40,"style":40},"Enter your age: 25\n25\n\u003Cclass 'int'>\n",[15,565,566,574,578],{"__ignoreMap":40},[44,567,568,570,572],{"class":46,"line":47},[44,569,446],{"class":50},[44,571,449],{"class":62},[44,573,453],{"class":452},[44,575,576],{"class":46,"line":79},[44,577,458],{"class":452},[44,579,580,582,584,586,589,591],{"class":46,"line":419},[44,581,463],{"class":54},[44,583,467],{"class":466},[44,585,470],{"class":66},[44,587,588],{"class":70},"int",[44,590,476],{"class":66},[44,592,479],{"class":54},[108,594],{},[30,596,598],{"id":597},"simple-examples-beginners-need","Simple examples beginners need",[600,601,603],"h3",{"id":602},"ask-for-a-name-and-print-it","Ask for a name and print it",[35,605,607],{"className":37,"code":606,"language":39,"meta":40,"style":40},"name = input(\"What is your name? \")\nprint(\"Hello,\", name)\n",[15,608,609,628],{"__ignoreMap":40},[44,610,611,613,615,617,619,621,624,626],{"class":46,"line":47},[44,612,51],{"class":50},[44,614,55],{"class":54},[44,616,59],{"class":58},[44,618,63],{"class":62},[44,620,67],{"class":66},[44,622,623],{"class":70},"What is your name? ",[44,625,67],{"class":66},[44,627,76],{"class":62},[44,629,630,632,634,636,639,641,643,645],{"class":46,"line":79},[44,631,82],{"class":58},[44,633,63],{"class":62},[44,635,67],{"class":66},[44,637,638],{"class":70},"Hello,",[44,640,67],{"class":66},[44,642,186],{"class":62},[44,644,98],{"class":97},[44,646,76],{"class":62},[20,648,649,650,653],{},"This stores the user's answer in ",[15,651,652],{},"name"," and prints it.",[600,655,657,658],{"id":656},"ask-for-age-and-convert-it-with-int","Ask for age and convert it with ",[15,659,496],{},[35,661,663],{"className":37,"code":662,"language":39,"meta":40,"style":40},"age = int(input(\"How old are you? \"))\nprint(\"Next year you will be\", age + 1)\n",[15,664,665,688],{"__ignoreMap":40},[44,666,667,669,671,673,675,677,679,681,684,686],{"class":46,"line":47},[44,668,390],{"class":50},[44,670,55],{"class":54},[44,672,520],{"class":426},[44,674,63],{"class":62},[44,676,263],{"class":58},[44,678,63],{"class":62},[44,680,67],{"class":66},[44,682,683],{"class":70},"How old are you? ",[44,685,67],{"class":66},[44,687,434],{"class":62},[44,689,690,692,694,696,699,701,703,706,709,712],{"class":46,"line":79},[44,691,82],{"class":58},[44,693,63],{"class":62},[44,695,67],{"class":66},[44,697,698],{"class":70},"Next year you will be",[44,700,67],{"class":66},[44,702,186],{"class":62},[44,704,705],{"class":97}," age ",[44,707,708],{"class":54},"+",[44,710,711],{"class":452}," 1",[44,713,76],{"class":62},[20,715,716,717,719,720,722],{},"This works because ",[15,718,496],{}," converts the text from ",[15,721,17],{}," into a whole number.",[20,724,725,726,504],{},"If you want more help with this step, see ",[491,727,729],{"href":728},"\u002Fhow-to\u002Fhow-to-convert-user-input-to-numbers-in-python\u002F","how to convert user input to numbers in Python",[600,731,733],{"id":732},"ask-for-two-numbers-and-add-them-after-conversion","Ask for two numbers and add them after conversion",[35,735,737],{"className":37,"code":736,"language":39,"meta":40,"style":40},"num1 = int(input(\"Enter the first number: \"))\nnum2 = int(input(\"Enter the second number: \"))\ntotal = num1 + num2\n\nprint(\"Total:\", total)\n",[15,738,739,763,787,802,809],{"__ignoreMap":40},[44,740,741,744,746,748,750,752,754,756,759,761],{"class":46,"line":47},[44,742,743],{"class":50},"num1 ",[44,745,55],{"class":54},[44,747,520],{"class":426},[44,749,63],{"class":62},[44,751,263],{"class":58},[44,753,63],{"class":62},[44,755,67],{"class":66},[44,757,758],{"class":70},"Enter the first number: ",[44,760,67],{"class":66},[44,762,434],{"class":62},[44,764,765,768,770,772,774,776,778,780,783,785],{"class":46,"line":79},[44,766,767],{"class":50},"num2 ",[44,769,55],{"class":54},[44,771,520],{"class":426},[44,773,63],{"class":62},[44,775,263],{"class":58},[44,777,63],{"class":62},[44,779,67],{"class":66},[44,781,782],{"class":70},"Enter the second number: ",[44,784,67],{"class":66},[44,786,434],{"class":62},[44,788,789,792,794,797,799],{"class":46,"line":419},[44,790,791],{"class":50},"total ",[44,793,55],{"class":54},[44,795,796],{"class":50}," num1 ",[44,798,708],{"class":54},[44,800,801],{"class":50}," num2\n",[44,803,805],{"class":46,"line":804},4,[44,806,808],{"emptyLinePlaceholder":807},true,"\n",[44,810,812,814,816,818,821,823,825,828],{"class":46,"line":811},5,[44,813,82],{"class":58},[44,815,63],{"class":62},[44,817,67],{"class":66},[44,819,820],{"class":70},"Total:",[44,822,67],{"class":66},[44,824,186],{"class":62},[44,826,827],{"class":97}," total",[44,829,76],{"class":62},[20,831,194],{},[35,833,835],{"className":37,"code":834,"language":39,"meta":40,"style":40},"Enter the first number: 2\nEnter the second number: 3\nTotal: 5\n",[15,836,837,847,857],{"__ignoreMap":40},[44,838,839,842,844],{"class":46,"line":47},[44,840,841],{"class":50},"Enter the first number",[44,843,449],{"class":62},[44,845,846],{"class":452}," 2\n",[44,848,849,852,854],{"class":46,"line":79},[44,850,851],{"class":50},"Enter the second number",[44,853,449],{"class":62},[44,855,856],{"class":452}," 3\n",[44,858,859,862,864],{"class":46,"line":419},[44,860,861],{"class":50},"Total",[44,863,449],{"class":62},[44,865,866],{"class":452}," 5\n",[20,868,869],{},"If you skip the conversion, Python joins the values as strings instead of adding them as numbers.",[20,871,145],{},[35,873,875],{"className":37,"code":874,"language":39,"meta":40,"style":40},"num1 = input(\"Enter the first number: \")\nnum2 = input(\"Enter the second number: \")\n\nprint(num1 + num2)\n",[15,876,877,895,913,917],{"__ignoreMap":40},[44,878,879,881,883,885,887,889,891,893],{"class":46,"line":47},[44,880,743],{"class":50},[44,882,55],{"class":54},[44,884,59],{"class":58},[44,886,63],{"class":62},[44,888,67],{"class":66},[44,890,758],{"class":70},[44,892,67],{"class":66},[44,894,76],{"class":62},[44,896,897,899,901,903,905,907,909,911],{"class":46,"line":79},[44,898,767],{"class":50},[44,900,55],{"class":54},[44,902,59],{"class":58},[44,904,63],{"class":62},[44,906,67],{"class":66},[44,908,782],{"class":70},[44,910,67],{"class":66},[44,912,76],{"class":62},[44,914,915],{"class":46,"line":419},[44,916,808],{"emptyLinePlaceholder":807},[44,918,919,921,923,925,927,930],{"class":46,"line":804},[44,920,82],{"class":58},[44,922,63],{"class":62},[44,924,743],{"class":97},[44,926,708],{"class":54},[44,928,929],{"class":97}," num2",[44,931,76],{"class":62},[20,933,194],{},[35,935,937],{"className":37,"code":936,"language":39,"meta":40,"style":40},"Enter the first number: 2\nEnter the second number: 3\n23\n",[15,938,939,947,955],{"__ignoreMap":40},[44,940,941,943,945],{"class":46,"line":47},[44,942,841],{"class":50},[44,944,449],{"class":62},[44,946,846],{"class":452},[44,948,949,951,953],{"class":46,"line":79},[44,950,851],{"class":50},[44,952,449],{"class":62},[44,954,856],{"class":452},[44,956,957],{"class":46,"line":419},[44,958,959],{"class":452},"23\n",[20,961,962,963,966,967,504],{},"That happens because ",[15,964,965],{},"\"2\" + \"3\""," creates ",[15,968,969],{},"\"23\"",[108,971],{},[30,973,975],{"id":974},"common-beginner-problems","Common beginner problems",[20,977,978,979,504],{},"Beginners often run into the same few issues with ",[15,980,17],{},[600,982,984,985,987],{"id":983},"forgetting-that-input-returns-a-string","Forgetting that ",[15,986,17],{}," returns a string",[20,989,990],{},"This is the most common mistake.",[35,992,994],{"className":37,"code":993,"language":39,"meta":40,"style":40},"value = input(\"Enter a number: \")\nprint(type(value))\n",[15,995,996,1015],{"__ignoreMap":40},[44,997,998,1000,1002,1004,1006,1008,1011,1013],{"class":46,"line":47},[44,999,338],{"class":50},[44,1001,55],{"class":54},[44,1003,59],{"class":58},[44,1005,63],{"class":62},[44,1007,67],{"class":66},[44,1009,1010],{"class":70},"Enter a number: ",[44,1012,67],{"class":66},[44,1014,76],{"class":62},[44,1016,1017,1019,1021,1023,1025,1027],{"class":46,"line":79},[44,1018,82],{"class":58},[44,1020,63],{"class":62},[44,1022,427],{"class":426},[44,1024,63],{"class":62},[44,1026,354],{"class":97},[44,1028,434],{"class":62},[20,1030,1031],{},"Output:",[35,1033,1035],{"className":37,"code":1034,"language":39,"meta":40,"style":40},"\u003Cclass 'str'>\n",[15,1036,1037],{"__ignoreMap":40},[44,1038,1039,1041,1043,1045,1047,1049],{"class":46,"line":47},[44,1040,463],{"class":54},[44,1042,467],{"class":466},[44,1044,470],{"class":66},[44,1046,473],{"class":70},[44,1048,476],{"class":66},[44,1050,479],{"class":54},[20,1052,1053],{},"If you need a number, convert it first.",[600,1055,1057],{"id":1056},"trying-to-add-numbers-without-converting-them","Trying to add numbers without converting them",[20,1059,1060],{},"This gives string joining, not math.",[35,1062,1064],{"className":37,"code":1063,"language":39,"meta":40,"style":40},"a = input(\"Enter a number: \")\nb = input(\"Enter another number: \")\nprint(a + b)\n",[15,1065,1066,1085,1105],{"__ignoreMap":40},[44,1067,1068,1071,1073,1075,1077,1079,1081,1083],{"class":46,"line":47},[44,1069,1070],{"class":50},"a ",[44,1072,55],{"class":54},[44,1074,59],{"class":58},[44,1076,63],{"class":62},[44,1078,67],{"class":66},[44,1080,1010],{"class":70},[44,1082,67],{"class":66},[44,1084,76],{"class":62},[44,1086,1087,1090,1092,1094,1096,1098,1101,1103],{"class":46,"line":79},[44,1088,1089],{"class":50},"b ",[44,1091,55],{"class":54},[44,1093,59],{"class":58},[44,1095,63],{"class":62},[44,1097,67],{"class":66},[44,1099,1100],{"class":70},"Enter another number: ",[44,1102,67],{"class":66},[44,1104,76],{"class":62},[44,1106,1107,1109,1111,1113,1115,1118],{"class":46,"line":419},[44,1108,82],{"class":58},[44,1110,63],{"class":62},[44,1112,1070],{"class":97},[44,1114,708],{"class":54},[44,1116,1117],{"class":97}," b",[44,1119,76],{"class":62},[20,1121,1122,1123,1126,1127,1130,1131,1134,1135,504],{},"If the user enters ",[15,1124,1125],{},"4"," and ",[15,1128,1129],{},"5",", the result is ",[15,1132,1133],{},"45",", not ",[15,1136,1137],{},"9",[600,1139,1141,1142,1145,1146],{"id":1140},"getting-valueerror-when-converting-invalid-text-to-int","Getting ",[15,1143,1144],{},"ValueError"," when converting invalid text to ",[15,1147,496],{},[20,1149,145],{},[35,1151,1153],{"className":37,"code":1152,"language":39,"meta":40,"style":40},"age = int(input(\"Enter your age: \"))\n",[15,1154,1155],{"__ignoreMap":40},[44,1156,1157,1159,1161,1163,1165,1167,1169,1171,1173,1175],{"class":46,"line":47},[44,1158,390],{"class":50},[44,1160,55],{"class":54},[44,1162,520],{"class":426},[44,1164,63],{"class":62},[44,1166,263],{"class":58},[44,1168,63],{"class":62},[44,1170,67],{"class":66},[44,1172,401],{"class":70},[44,1174,67],{"class":66},[44,1176,434],{"class":62},[20,1178,1179,1180,1183,1184,504],{},"If the user types ",[15,1181,1182],{},"ten",", Python raises a ",[15,1185,1144],{},[20,1187,1188,1189,504],{},"If you see this problem, read ",[491,1190,1192],{"href":1191},"\u002Ferrors\u002Fvalueerror-invalid-literal-for-int-with-base-10-fix\u002F","ValueError: invalid literal for int() with base 10",[600,1194,1196],{"id":1195},"leaving-extra-spaces-in-user-input","Leaving extra spaces in user input",[20,1198,1199],{},"Users sometimes type extra spaces before or after their answer.",[35,1201,1203],{"className":37,"code":1202,"language":39,"meta":40,"style":40},"name = input(\"Enter your name: \")\nprint(repr(name))\n",[15,1204,1205,1223],{"__ignoreMap":40},[44,1206,1207,1209,1211,1213,1215,1217,1219,1221],{"class":46,"line":47},[44,1208,51],{"class":50},[44,1210,55],{"class":54},[44,1212,59],{"class":58},[44,1214,63],{"class":62},[44,1216,67],{"class":66},[44,1218,71],{"class":70},[44,1220,67],{"class":66},[44,1222,76],{"class":62},[44,1224,1225,1227,1229,1232,1234,1236],{"class":46,"line":79},[44,1226,82],{"class":58},[44,1228,63],{"class":62},[44,1230,1231],{"class":58},"repr",[44,1233,63],{"class":62},[44,1235,652],{"class":97},[44,1237,434],{"class":62},[20,1239,1240],{},"If the user enters spaces, those spaces become part of the string.",[20,1242,1243,1244,449],{},"You can remove them with ",[15,1245,1246],{},".strip()",[35,1248,1250],{"className":37,"code":1249,"language":39,"meta":40,"style":40},"name = input(\"Enter your name: \").strip()\nprint(name)\n",[15,1251,1252,1276],{"__ignoreMap":40},[44,1253,1254,1256,1258,1260,1262,1264,1266,1268,1271,1274],{"class":46,"line":47},[44,1255,51],{"class":50},[44,1257,55],{"class":54},[44,1259,59],{"class":58},[44,1261,63],{"class":62},[44,1263,67],{"class":66},[44,1265,71],{"class":70},[44,1267,67],{"class":66},[44,1269,1270],{"class":62},").",[44,1272,1273],{"class":97},"strip",[44,1275,345],{"class":62},[44,1277,1278,1280,1282,1284],{"class":46,"line":79},[44,1279,82],{"class":58},[44,1281,63],{"class":62},[44,1283,652],{"class":97},[44,1285,76],{"class":62},[108,1287],{},[30,1289,1291],{"id":1290},"helpful-tips","Helpful tips",[20,1293,1294,1295,1297],{},"These habits make ",[15,1296,17],{}," easier and safer to use.",[126,1299,1300,1303,1308,1311],{},[129,1301,1302],{},"Use clear prompts so users know what to enter",[129,1304,103,1305,1307],{},[15,1306,1246],{}," to remove extra spaces when needed",[129,1309,1310],{},"Validate input before using it in larger programs",[129,1312,103,1313,1316],{},[15,1314,1315],{},"try-except"," when converting input to numbers",[20,1318,1319,1320,449],{},"Example with ",[15,1321,1246],{},[35,1323,1325],{"className":37,"code":1324,"language":39,"meta":40,"style":40},"city = input(\"Enter your city: \").strip()\nprint(\"City:\", city)\n",[15,1326,1327,1351],{"__ignoreMap":40},[44,1328,1329,1332,1334,1336,1338,1340,1343,1345,1347,1349],{"class":46,"line":47},[44,1330,1331],{"class":50},"city ",[44,1333,55],{"class":54},[44,1335,59],{"class":58},[44,1337,63],{"class":62},[44,1339,67],{"class":66},[44,1341,1342],{"class":70},"Enter your city: ",[44,1344,67],{"class":66},[44,1346,1270],{"class":62},[44,1348,1273],{"class":97},[44,1350,345],{"class":62},[44,1352,1353,1355,1357,1359,1362,1364,1366,1369],{"class":46,"line":79},[44,1354,82],{"class":58},[44,1356,63],{"class":62},[44,1358,67],{"class":66},[44,1360,1361],{"class":70},"City:",[44,1363,67],{"class":66},[44,1365,186],{"class":62},[44,1367,1368],{"class":97}," city",[44,1370,76],{"class":62},[20,1372,1319,1373,449],{},[15,1374,1315],{},[35,1376,1378],{"className":37,"code":1377,"language":39,"meta":40,"style":40},"try:\n    age = int(input(\"Enter your age: \"))\n    print(\"You will be\", age + 1, \"next year\")\nexcept ValueError:\n    print(\"Please enter a whole number.\")\n",[15,1379,1380,1389,1412,1446,1456],{"__ignoreMap":40},[44,1381,1382,1386],{"class":46,"line":47},[44,1383,1385],{"class":1384},"sVHd0","try",[44,1387,1388],{"class":62},":\n",[44,1390,1391,1394,1396,1398,1400,1402,1404,1406,1408,1410],{"class":46,"line":79},[44,1392,1393],{"class":50},"    age ",[44,1395,55],{"class":54},[44,1397,520],{"class":426},[44,1399,63],{"class":62},[44,1401,263],{"class":58},[44,1403,63],{"class":62},[44,1405,67],{"class":66},[44,1407,401],{"class":70},[44,1409,67],{"class":66},[44,1411,434],{"class":62},[44,1413,1414,1417,1419,1421,1424,1426,1428,1430,1432,1434,1436,1439,1442,1444],{"class":46,"line":419},[44,1415,1416],{"class":58},"    print",[44,1418,63],{"class":62},[44,1420,67],{"class":66},[44,1422,1423],{"class":70},"You will be",[44,1425,67],{"class":66},[44,1427,186],{"class":62},[44,1429,705],{"class":97},[44,1431,708],{"class":54},[44,1433,711],{"class":452},[44,1435,186],{"class":62},[44,1437,1438],{"class":66}," \"",[44,1440,1441],{"class":70},"next year",[44,1443,67],{"class":66},[44,1445,76],{"class":62},[44,1447,1448,1451,1454],{"class":46,"line":804},[44,1449,1450],{"class":1384},"except",[44,1452,1453],{"class":426}," ValueError",[44,1455,1388],{"class":62},[44,1457,1458,1460,1462,1464,1467,1469],{"class":46,"line":811},[44,1459,1416],{"class":58},[44,1461,63],{"class":62},[44,1463,67],{"class":66},[44,1465,1466],{"class":70},"Please enter a whole number.",[44,1468,67],{"class":66},[44,1470,76],{"class":62},[20,1472,1473],{},"This prevents the program from crashing when the user enters invalid text.",[20,1475,1476,1477,504],{},"If you are new to interactive programs, see ",[491,1478,1480],{"href":1479},"\u002Fhow-to\u002Fhow-to-get-user-input-in-python\u002F","how to get user input in Python",[108,1482],{},[30,1484,1486],{"id":1485},"common-mistakes","Common mistakes",[20,1488,1489,1490,449],{},"These are the most common causes of problems when using ",[15,1491,17],{},[126,1493,1494,1501,1504,1507],{},[129,1495,1496,1497,497,1499],{},"Treating user input as a number without using ",[15,1498,496],{},[15,1500,503],{},[129,1502,1503],{},"Concatenating strings and numbers together incorrectly",[129,1505,1506],{},"Entering non-numeric text when the program expects a number",[129,1508,1509],{},"Assuming pressing Enter gives a number instead of an empty string",[20,1511,1512],{},"Useful debugging checks:",[35,1514,1516],{"className":37,"code":1515,"language":39,"meta":40,"style":40},"print(value)\nprint(type(value))\nprint(repr(value))\n",[15,1517,1518,1528,1542],{"__ignoreMap":40},[44,1519,1520,1522,1524,1526],{"class":46,"line":47},[44,1521,82],{"class":58},[44,1523,63],{"class":62},[44,1525,354],{"class":97},[44,1527,76],{"class":62},[44,1529,1530,1532,1534,1536,1538,1540],{"class":46,"line":79},[44,1531,82],{"class":58},[44,1533,63],{"class":62},[44,1535,427],{"class":426},[44,1537,63],{"class":62},[44,1539,354],{"class":97},[44,1541,434],{"class":62},[44,1543,1544,1546,1548,1550,1552,1554],{"class":46,"line":419},[44,1545,82],{"class":58},[44,1547,63],{"class":62},[44,1549,1231],{"class":58},[44,1551,63],{"class":62},[44,1553,354],{"class":97},[44,1555,434],{"class":62},[20,1557,1558,1559,1561],{},"These help you inspect what ",[15,1560,17],{}," actually returned:",[126,1563,1564,1570,1576],{},[129,1565,1566,1569],{},[15,1567,1568],{},"print(value)"," shows the value",[129,1571,1572,1575],{},[15,1573,1574],{},"print(type(value))"," shows the data type",[129,1577,1578,1581],{},[15,1579,1580],{},"print(repr(value))"," shows hidden spaces and special characters",[20,1583,145],{},[35,1585,1587],{"className":37,"code":1586,"language":39,"meta":40,"style":40},"value = input(\"Enter something: \")\nprint(value)\nprint(type(value))\nprint(repr(value))\n",[15,1588,1589,1608,1618,1632],{"__ignoreMap":40},[44,1590,1591,1593,1595,1597,1599,1601,1604,1606],{"class":46,"line":47},[44,1592,338],{"class":50},[44,1594,55],{"class":54},[44,1596,59],{"class":58},[44,1598,63],{"class":62},[44,1600,67],{"class":66},[44,1602,1603],{"class":70},"Enter something: ",[44,1605,67],{"class":66},[44,1607,76],{"class":62},[44,1609,1610,1612,1614,1616],{"class":46,"line":79},[44,1611,82],{"class":58},[44,1613,63],{"class":62},[44,1615,354],{"class":97},[44,1617,76],{"class":62},[44,1619,1620,1622,1624,1626,1628,1630],{"class":46,"line":419},[44,1621,82],{"class":58},[44,1623,63],{"class":62},[44,1625,427],{"class":426},[44,1627,63],{"class":62},[44,1629,354],{"class":97},[44,1631,434],{"class":62},[44,1633,1634,1636,1638,1640,1642,1644],{"class":46,"line":804},[44,1635,82],{"class":58},[44,1637,63],{"class":62},[44,1639,1231],{"class":58},[44,1641,63],{"class":62},[44,1643,354],{"class":97},[44,1645,434],{"class":62},[108,1647],{},[30,1649,1651],{"id":1650},"faq","FAQ",[600,1653,1655,1656,1658],{"id":1654},"does-input-return-an-integer","Does ",[15,1657,17],{}," return an integer?",[20,1660,1661,1662,1664,1665,1667],{},"No. ",[15,1663,17],{}," always returns a string. Convert it with ",[15,1666,496],{}," if needed.",[600,1669,1671,1672,213],{"id":1670},"is-the-prompt-required-in-input","Is the prompt required in ",[15,1673,17],{},[20,1675,1676,1677,1679],{},"No. You can call ",[15,1678,17],{}," with no prompt, but prompts help users know what to type.",[600,1681,1683,1684,1687,1688,213],{"id":1682},"why-does-2-3-not-work-correctly-with-input","Why does ",[15,1685,1686],{},"2 + 3"," not work correctly with ",[15,1689,17],{},[20,1691,1692,1693,1695,1696,1698,1699,1701],{},"Because ",[15,1694,17],{}," returns strings. ",[15,1697,965],{}," becomes ",[15,1700,969],{}," unless you convert both values to numbers.",[600,1703,1705],{"id":1704},"how-do-i-remove-spaces-from-input","How do I remove spaces from input?",[20,1707,103,1708,1710,1711,504],{},[15,1709,1246],{}," on the returned string, such as ",[15,1712,1713],{},"input(\"Name: \").strip()",[108,1715],{},[30,1717,1719],{"id":1718},"see-also","See also",[126,1721,1722,1727,1732,1739,1745,1749],{},[129,1723,1724],{},[491,1725,1726],{"href":1479},"How to get user input in Python",[129,1728,1729],{},[491,1730,1731],{"href":728},"How to convert user input to numbers in Python",[129,1733,1734],{},[491,1735,13,1736,1738],{"href":493},[15,1737,496],{}," function explained",[129,1740,1741],{},[491,1742,13,1743,1738],{"href":500},[15,1744,503],{},[129,1746,1747],{},[491,1748,1192],{"href":1191},[129,1750,1751],{},[491,1752,13,1754,1738],{"href":1753},"\u002Freference\u002Fpython-print-function-explained\u002F",[15,1755,1756],{},"print()",[1758,1759,1760],"style",{},"html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .srjyR, html code.shiki .srjyR{--shiki-light:#90A4AE;--shiki-light-font-style:inherit;--shiki-default:#B31D28;--shiki-default-font-style:italic;--shiki-dark:#FDAEB7;--shiki-dark-font-style:italic}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}",{"title":40,"searchDepth":79,"depth":79,"links":1762},[1763,1764,1766,1767,1769,1775,1783,1784,1785,1794],{"id":32,"depth":79,"text":33},{"id":112,"depth":79,"text":1765},"What input() does",{"id":249,"depth":79,"text":250},{"id":367,"depth":79,"text":1768},"What input() returns",{"id":597,"depth":79,"text":598,"children":1770},[1771,1772,1774],{"id":602,"depth":419,"text":603},{"id":656,"depth":419,"text":1773},"Ask for age and convert it with int()",{"id":732,"depth":419,"text":733},{"id":974,"depth":79,"text":975,"children":1776},[1777,1779,1780,1782],{"id":983,"depth":419,"text":1778},"Forgetting that input() returns a string",{"id":1056,"depth":419,"text":1057},{"id":1140,"depth":419,"text":1781},"Getting ValueError when converting invalid text to int()",{"id":1195,"depth":419,"text":1196},{"id":1290,"depth":79,"text":1291},{"id":1485,"depth":79,"text":1486},{"id":1650,"depth":79,"text":1651,"children":1786},[1787,1789,1791,1793],{"id":1654,"depth":419,"text":1788},"Does input() return an integer?",{"id":1670,"depth":419,"text":1790},"Is the prompt required in input()?",{"id":1682,"depth":419,"text":1792},"Why does 2 + 3 not work correctly with input()?",{"id":1704,"depth":419,"text":1705},{"id":1718,"depth":79,"text":1719},"Master python input function explained in our comprehensive Python beginner guide.","md",{},"\u002Freference\u002Fpython-input-function-explained",{"title":5,"description":1795},"reference\u002Fpython-input-function-explained","mgCYTLqY7_xdXYb-uIqvqqWNrZiPlmc9q8zaEr1fEyw",1777585500689]