[{"data":1,"prerenderedAt":1611},["ShallowReactive",2],{"doc-\u002Fhow-to\u002Fhow-to-get-user-input-in-python":3},{"id":4,"title":5,"body":6,"description":1604,"extension":1605,"meta":1606,"navigation":711,"path":1607,"seo":1608,"stem":1609,"__hash__":1610},"content\u002Fhow-to\u002Fhow-to-get-user-input-in-python.md","How to Get User Input in Python",{"type":7,"value":8,"toc":1571},"minimark",[9,13,22,25,46,51,118,137,141,158,164,169,181,187,190,224,227,249,259,263,268,282,285,330,333,341,344,346,382,391,395,408,415,475,477,483,489,539,541,547,550,575,582,586,592,594,632,642,652,656,659,755,758,764,767,786,790,798,808,891,894,914,917,923,935,939,946,949,986,992,995,1036,1040,1043,1046,1098,1101,1152,1159,1162,1165,1199,1202,1242,1246,1249,1252,1277,1280,1304,1308,1311,1314,1384,1387,1410,1417,1465,1469,1476,1479,1483,1495,1499,1509,1513,1519,1523,1564,1567],[10,11,5],"h1",{"id":12},"how-to-get-user-input-in-python",[14,15,16,17,21],"p",{},"If you want your Python program to ask the user for a name, age, or any other value, use ",[18,19,20],"code",{},"input()",".",[14,23,24],{},"This page shows you how to:",[26,27,28,34,37,40,43],"ul",{},[29,30,31,32],"li",{},"ask for input with ",[18,33,20],{},[29,35,36],{},"store the result in a variable",[29,38,39],{},"print the value back to the user",[29,41,42],{},"convert input text to numbers when needed",[29,44,45],{},"avoid common beginner mistakes",[47,48,50],"h2",{"id":49},"quick-answer","Quick answer",[52,53,58],"pre",{"className":54,"code":55,"language":56,"meta":57,"style":57},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","name = input(\"Enter your name: \")\nprint(\"Hello,\", name)\n","python","",[18,59,60,94],{"__ignoreMap":57},[61,62,65,69,73,77,81,85,89,91],"span",{"class":63,"line":64},"line",1,[61,66,68],{"class":67},"su5hD","name ",[61,70,72],{"class":71},"smGrS","=",[61,74,76],{"class":75},"sptTA"," input",[61,78,80],{"class":79},"sP7_E","(",[61,82,84],{"class":83},"sjJ54","\"",[61,86,88],{"class":87},"s_sjI","Enter your name: ",[61,90,84],{"class":83},[61,92,93],{"class":79},")\n",[61,95,97,100,102,104,107,109,112,116],{"class":63,"line":96},2,[61,98,99],{"class":75},"print",[61,101,80],{"class":79},[61,103,84],{"class":83},[61,105,106],{"class":87},"Hello,",[61,108,84],{"class":83},[61,110,111],{"class":79},",",[61,113,115],{"class":114},"slqww"," name",[61,117,93],{"class":79},[14,119,120,122,123,130,131,21],{},[18,121,20],{}," always returns text. If you need a number, convert it with ",[124,125,127],"a",{"href":126},"\u002Freference\u002Fpython-int-function-explained\u002F",[18,128,129],{},"int()"," or ",[124,132,134],{"href":133},"\u002Freference\u002Fpython-float-function-explained\u002F",[18,135,136],{},"float()",[47,138,140],{"id":139},"what-this-page-helps-you-do","What this page helps you do",[26,142,143,149,152,155],{},[29,144,145,146,148],{},"Use ",[18,147,20],{}," to read text from the keyboard",[29,150,151],{},"Store the result in a variable",[29,153,154],{},"Print the entered value",[29,156,157],{},"Convert input text to numbers when needed",[47,159,161,162],{"id":160},"basic-syntax-of-input","Basic syntax of ",[18,163,20],{},[14,165,166,168],{},[18,167,20],{}," pauses the program and waits for the user to type something.",[14,170,171,172,175,176,180],{},"The text inside ",[18,173,174],{},"input(\"...\")"," is called the ",[177,178,179],"strong",{},"prompt",". It tells the user what to enter.",[14,182,183,184,186],{},"The value returned by ",[18,185,20],{}," is always a string. A string is text in Python.",[14,188,189],{},"Basic example:",[52,191,193],{"className":54,"code":192,"language":56,"meta":57,"style":57},"name = input(\"Enter your name: \")\nprint(name)\n",[18,194,195,213],{"__ignoreMap":57},[61,196,197,199,201,203,205,207,209,211],{"class":63,"line":64},[61,198,68],{"class":67},[61,200,72],{"class":71},[61,202,76],{"class":75},[61,204,80],{"class":79},[61,206,84],{"class":83},[61,208,88],{"class":87},[61,210,84],{"class":83},[61,212,93],{"class":79},[61,214,215,217,219,222],{"class":63,"line":96},[61,216,99],{"class":75},[61,218,80],{"class":79},[61,220,221],{"class":114},"name",[61,223,93],{"class":79},[14,225,226],{},"What happens here:",[26,228,229,235,238,243],{},[29,230,231,234],{},[18,232,233],{},"input(\"Enter your name: \")"," shows a message",[29,236,237],{},"the program waits for the user to type",[29,239,240,241],{},"the typed value is stored in ",[18,242,221],{},[29,244,245,248],{},[18,246,247],{},"print(name)"," shows the value",[14,250,251,252,21],{},"If you want a full explanation of how this function works, see ",[124,253,255,256,258],{"href":254},"\u002Freference\u002Fpython-input-function-explained\u002F","Python ",[18,257,20],{}," function explained",[47,260,262],{"id":261},"get-text-input","Get text input",[14,264,145,265,267],{},[18,266,20],{}," directly when you want text such as:",[26,269,270,273,276,279],{},[29,271,272],{},"names",[29,274,275],{},"cities",[29,277,278],{},"favorite colors",[29,280,281],{},"yes\u002Fno answers",[14,283,284],{},"Example:",[52,286,288],{"className":54,"code":287,"language":56,"meta":57,"style":57},"city = input(\"Which city do you live in? \")\nprint(\"You live in\", city)\n",[18,289,290,310],{"__ignoreMap":57},[61,291,292,295,297,299,301,303,306,308],{"class":63,"line":64},[61,293,294],{"class":67},"city ",[61,296,72],{"class":71},[61,298,76],{"class":75},[61,300,80],{"class":79},[61,302,84],{"class":83},[61,304,305],{"class":87},"Which city do you live in? ",[61,307,84],{"class":83},[61,309,93],{"class":79},[61,311,312,314,316,318,321,323,325,328],{"class":63,"line":96},[61,313,99],{"class":75},[61,315,80],{"class":79},[61,317,84],{"class":83},[61,319,320],{"class":87},"You live in",[61,322,84],{"class":83},[61,324,111],{"class":79},[61,326,327],{"class":114}," city",[61,329,93],{"class":79},[14,331,332],{},"Possible output:",[52,334,339],{"className":335,"code":337,"language":338,"meta":57},[336],"language-text","Which city do you live in? New York\nYou live in New York\n","text",[18,340,337],{"__ignoreMap":57},[14,342,343],{},"If the user types spaces, they stay in the result.",[14,345,284],{},[52,347,349],{"className":54,"code":348,"language":56,"meta":57,"style":57},"full_name = input(\"Enter your full name: \")\nprint(full_name)\n",[18,350,351,371],{"__ignoreMap":57},[61,352,353,356,358,360,362,364,367,369],{"class":63,"line":64},[61,354,355],{"class":67},"full_name ",[61,357,72],{"class":71},[61,359,76],{"class":75},[61,361,80],{"class":79},[61,363,84],{"class":83},[61,365,366],{"class":87},"Enter your full name: ",[61,368,84],{"class":83},[61,370,93],{"class":79},[61,372,373,375,377,380],{"class":63,"line":96},[61,374,99],{"class":75},[61,376,80],{"class":79},[61,378,379],{"class":114},"full_name",[61,381,93],{"class":79},[14,383,384,385,388,389,21],{},"If the user enters ",[18,386,387],{},"Ava Smith",", the variable contains the full text ",[18,390,387],{},[47,392,394],{"id":393},"convert-input-to-a-number","Convert input to a number",[14,396,397,399,400,403,404,407],{},[18,398,20],{}," does ",[177,401,402],{},"not"," return numbers by itself. Even if the user types ",[18,405,406],{},"25",", Python still treats it as text.",[409,410,145,412,414],"h3",{"id":411},"use-int-for-whole-numbers",[18,413,129],{}," for whole numbers",[52,416,418],{"className":54,"code":417,"language":56,"meta":57,"style":57},"age = int(input(\"Enter your age: \"))\nprint(\"Next year you will be\", age + 1)\n",[18,419,420,448],{"__ignoreMap":57},[61,421,422,425,427,431,433,436,438,440,443,445],{"class":63,"line":64},[61,423,424],{"class":67},"age ",[61,426,72],{"class":71},[61,428,430],{"class":429},"sZMiF"," int",[61,432,80],{"class":79},[61,434,435],{"class":75},"input",[61,437,80],{"class":79},[61,439,84],{"class":83},[61,441,442],{"class":87},"Enter your age: ",[61,444,84],{"class":83},[61,446,447],{"class":79},"))\n",[61,449,450,452,454,456,459,461,463,466,469,473],{"class":63,"line":96},[61,451,99],{"class":75},[61,453,80],{"class":79},[61,455,84],{"class":83},[61,457,458],{"class":87},"Next year you will be",[61,460,84],{"class":83},[61,462,111],{"class":79},[61,464,465],{"class":114}," age ",[61,467,468],{"class":71},"+",[61,470,472],{"class":471},"srdBf"," 1",[61,474,93],{"class":79},[14,476,332],{},[52,478,481],{"className":479,"code":480,"language":338,"meta":57},[336],"Enter your age: 30\nNext year you will be 31\n",[18,482,480],{"__ignoreMap":57},[409,484,145,486,488],{"id":485},"use-float-for-decimal-numbers",[18,487,136],{}," for decimal numbers",[52,490,492],{"className":54,"code":491,"language":56,"meta":57,"style":57},"price = float(input(\"Enter the price: \"))\nprint(\"The price is\", price)\n",[18,493,494,519],{"__ignoreMap":57},[61,495,496,499,501,504,506,508,510,512,515,517],{"class":63,"line":64},[61,497,498],{"class":67},"price ",[61,500,72],{"class":71},[61,502,503],{"class":429}," float",[61,505,80],{"class":79},[61,507,435],{"class":75},[61,509,80],{"class":79},[61,511,84],{"class":83},[61,513,514],{"class":87},"Enter the price: ",[61,516,84],{"class":83},[61,518,447],{"class":79},[61,520,521,523,525,527,530,532,534,537],{"class":63,"line":96},[61,522,99],{"class":75},[61,524,80],{"class":79},[61,526,84],{"class":83},[61,528,529],{"class":87},"The price is",[61,531,84],{"class":83},[61,533,111],{"class":79},[61,535,536],{"class":114}," price",[61,538,93],{"class":79},[14,540,332],{},[52,542,545],{"className":543,"code":544,"language":338,"meta":57},[336],"Enter the price: 19.99\nThe price is 19.99\n",[18,546,544],{"__ignoreMap":57},[14,548,549],{},"Use:",[26,551,552,565],{},[29,553,554,558,559,130,562],{},[124,555,556],{"href":126},[18,557,129],{}," for whole numbers like ",[18,560,561],{},"5",[18,563,564],{},"42",[29,566,567,571,572],{},[124,568,569],{"href":133},[18,570,136],{}," for decimal numbers like ",[18,573,574],{},"3.14",[14,576,577,578,21],{},"If you want more examples, see ",[124,579,581],{"href":580},"\u002Fhow-to\u002Fhow-to-convert-user-input-to-numbers-in-python\u002F","how to convert user input to numbers in Python",[409,583,585],{"id":584},"important-warning","Important warning",[14,587,588,589,21],{},"If the user enters something that cannot be converted, Python raises a ",[18,590,591],{},"ValueError",[14,593,284],{},[52,595,597],{"className":54,"code":596,"language":56,"meta":57,"style":57},"age = int(input(\"Enter your age: \"))\nprint(age)\n",[18,598,599,621],{"__ignoreMap":57},[61,600,601,603,605,607,609,611,613,615,617,619],{"class":63,"line":64},[61,602,424],{"class":67},[61,604,72],{"class":71},[61,606,430],{"class":429},[61,608,80],{"class":79},[61,610,435],{"class":75},[61,612,80],{"class":79},[61,614,84],{"class":83},[61,616,442],{"class":87},[61,618,84],{"class":83},[61,620,447],{"class":79},[61,622,623,625,627,630],{"class":63,"line":96},[61,624,99],{"class":75},[61,626,80],{"class":79},[61,628,629],{"class":114},"age",[61,631,93],{"class":79},[14,633,634,635,638,639,641],{},"If the user types ",[18,636,637],{},"hello",", the program crashes because ",[18,640,637],{}," is not a valid integer.",[14,643,644,645,21],{},"For that specific problem, see ",[124,646,648,649],{"href":647},"\u002Ferrors\u002Fvalueerror-invalid-literal-for-int-with-base-10-fix\u002F","how to fix ",[18,650,651],{},"ValueError: invalid literal for int() with base 10",[47,653,655],{"id":654},"use-input-in-a-simple-program","Use input in a simple program",[14,657,658],{},"Here is a small program that asks for two values and prints a message.",[52,660,662],{"className":54,"code":661,"language":56,"meta":57,"style":57},"name = input(\"What is your name? \")\nage = int(input(\"How old are you? \"))\n\nprint(\"Hello,\", name)\nprint(\"Next year you will be\", age + 1)\n",[18,663,664,683,706,713,732],{"__ignoreMap":57},[61,665,666,668,670,672,674,676,679,681],{"class":63,"line":64},[61,667,68],{"class":67},[61,669,72],{"class":71},[61,671,76],{"class":75},[61,673,80],{"class":79},[61,675,84],{"class":83},[61,677,678],{"class":87},"What is your name? ",[61,680,84],{"class":83},[61,682,93],{"class":79},[61,684,685,687,689,691,693,695,697,699,702,704],{"class":63,"line":96},[61,686,424],{"class":67},[61,688,72],{"class":71},[61,690,430],{"class":429},[61,692,80],{"class":79},[61,694,435],{"class":75},[61,696,80],{"class":79},[61,698,84],{"class":83},[61,700,701],{"class":87},"How old are you? ",[61,703,84],{"class":83},[61,705,447],{"class":79},[61,707,709],{"class":63,"line":708},3,[61,710,712],{"emptyLinePlaceholder":711},true,"\n",[61,714,716,718,720,722,724,726,728,730],{"class":63,"line":715},4,[61,717,99],{"class":75},[61,719,80],{"class":79},[61,721,84],{"class":83},[61,723,106],{"class":87},[61,725,84],{"class":83},[61,727,111],{"class":79},[61,729,115],{"class":114},[61,731,93],{"class":79},[61,733,735,737,739,741,743,745,747,749,751,753],{"class":63,"line":734},5,[61,736,99],{"class":75},[61,738,80],{"class":79},[61,740,84],{"class":83},[61,742,458],{"class":87},[61,744,84],{"class":83},[61,746,111],{"class":79},[61,748,465],{"class":114},[61,750,468],{"class":71},[61,752,472],{"class":471},[61,754,93],{"class":79},[14,756,757],{},"Example run:",[52,759,762],{"className":760,"code":761,"language":338,"meta":57},[336],"What is your name? Maya\nHow old are you? 12\nHello, Maya\nNext year you will be 13\n",[18,763,761],{"__ignoreMap":57},[14,765,766],{},"This example combines:",[26,768,769,772,775,778],{},[29,770,771],{},"prompts",[29,773,774],{},"variables",[29,776,777],{},"number conversion",[29,779,780],{},[124,781,783],{"href":782},"\u002Freference\u002Fpython-print-function-explained\u002F",[18,784,785],{},"print()",[47,787,789],{"id":788},"handle-invalid-input-safely","Handle invalid input safely",[14,791,792,793,130,795,797],{},"If you convert user input with ",[18,794,129],{},[18,796,136],{},", bad input can cause an error.",[14,799,800,801,804,805,21],{},"A simple way to handle this is with ",[18,802,803],{},"try"," and ",[18,806,807],{},"except",[52,809,811],{"className":54,"code":810,"language":56,"meta":57,"style":57},"try:\n    age = int(input(\"Enter your age: \"))\n    print(\"Next year you will be\", age + 1)\nexcept ValueError:\n    print(\"Please enter a valid whole number.\")\n",[18,812,813,821,844,867,876],{"__ignoreMap":57},[61,814,815,818],{"class":63,"line":64},[61,816,803],{"class":817},"sVHd0",[61,819,820],{"class":79},":\n",[61,822,823,826,828,830,832,834,836,838,840,842],{"class":63,"line":96},[61,824,825],{"class":67},"    age ",[61,827,72],{"class":71},[61,829,430],{"class":429},[61,831,80],{"class":79},[61,833,435],{"class":75},[61,835,80],{"class":79},[61,837,84],{"class":83},[61,839,442],{"class":87},[61,841,84],{"class":83},[61,843,447],{"class":79},[61,845,846,849,851,853,855,857,859,861,863,865],{"class":63,"line":708},[61,847,848],{"class":75},"    print",[61,850,80],{"class":79},[61,852,84],{"class":83},[61,854,458],{"class":87},[61,856,84],{"class":83},[61,858,111],{"class":79},[61,860,465],{"class":114},[61,862,468],{"class":71},[61,864,472],{"class":471},[61,866,93],{"class":79},[61,868,869,871,874],{"class":63,"line":715},[61,870,807],{"class":817},[61,872,873],{"class":429}," ValueError",[61,875,820],{"class":79},[61,877,878,880,882,884,887,889],{"class":63,"line":734},[61,879,848],{"class":75},[61,881,80],{"class":79},[61,883,84],{"class":83},[61,885,886],{"class":87},"Please enter a valid whole number.",[61,888,84],{"class":83},[61,890,93],{"class":79},[14,892,893],{},"What this does:",[26,895,896,901,911],{},[29,897,898,900],{},[18,899,803],{}," runs the code that might fail",[29,902,903,904,906,907,910],{},"if ",[18,905,129],{}," cannot convert the text, ",[18,908,909],{},"except ValueError"," runs",[29,912,913],{},"the program shows a clear message instead of crashing",[14,915,916],{},"Example run with bad input:",[52,918,921],{"className":919,"code":920,"language":338,"meta":57},[336],"Enter your age: twelve\nPlease enter a valid whole number.\n",[18,922,920],{"__ignoreMap":57},[14,924,925,926,21],{},"If you are new to this, see ",[124,927,929,930,804,932,934],{"href":928},"\u002Fhow-to\u002Fhow-to-use-try-except-blocks-in-python\u002F","how to use ",[18,931,803],{},[18,933,807],{}," blocks in Python",[47,936,938],{"id":937},"common-beginner-mistakes","Common beginner mistakes",[409,940,942,943,945],{"id":941},"forgetting-that-input-returns-a-string","Forgetting that ",[18,944,20],{}," returns a string",[14,947,948],{},"This is one of the most common mistakes.",[52,950,952],{"className":54,"code":951,"language":56,"meta":57,"style":57},"age = input(\"Enter your age: \")\nprint(age + 1)\n",[18,953,954,972],{"__ignoreMap":57},[61,955,956,958,960,962,964,966,968,970],{"class":63,"line":64},[61,957,424],{"class":67},[61,959,72],{"class":71},[61,961,76],{"class":75},[61,963,80],{"class":79},[61,965,84],{"class":83},[61,967,442],{"class":87},[61,969,84],{"class":83},[61,971,93],{"class":79},[61,973,974,976,978,980,982,984],{"class":63,"line":96},[61,975,99],{"class":75},[61,977,80],{"class":79},[61,979,424],{"class":114},[61,981,468],{"class":71},[61,983,472],{"class":471},[61,985,93],{"class":79},[14,987,988,989,991],{},"This does not work because ",[18,990,629],{}," is text, not a number.",[14,993,994],{},"Fix it by converting the input:",[52,996,998],{"className":54,"code":997,"language":56,"meta":57,"style":57},"age = int(input(\"Enter your age: \"))\nprint(age + 1)\n",[18,999,1000,1022],{"__ignoreMap":57},[61,1001,1002,1004,1006,1008,1010,1012,1014,1016,1018,1020],{"class":63,"line":64},[61,1003,424],{"class":67},[61,1005,72],{"class":71},[61,1007,430],{"class":429},[61,1009,80],{"class":79},[61,1011,435],{"class":75},[61,1013,80],{"class":79},[61,1015,84],{"class":83},[61,1017,442],{"class":87},[61,1019,84],{"class":83},[61,1021,447],{"class":79},[61,1023,1024,1026,1028,1030,1032,1034],{"class":63,"line":96},[61,1025,99],{"class":75},[61,1027,80],{"class":79},[61,1029,424],{"class":114},[61,1031,468],{"class":71},[61,1033,472],{"class":471},[61,1035,93],{"class":79},[409,1037,1039],{"id":1038},"trying-to-add-a-number-to-raw-input-text","Trying to add a number to raw input text",[14,1041,1042],{},"If you mix strings and integers incorrectly, Python raises an error.",[14,1044,1045],{},"Wrong:",[52,1047,1049],{"className":54,"code":1048,"language":56,"meta":57,"style":57},"score = input(\"Enter your score: \")\nprint(\"Your score plus one is \" + (score + 1))\n",[18,1050,1051,1071],{"__ignoreMap":57},[61,1052,1053,1056,1058,1060,1062,1064,1067,1069],{"class":63,"line":64},[61,1054,1055],{"class":67},"score ",[61,1057,72],{"class":71},[61,1059,76],{"class":75},[61,1061,80],{"class":79},[61,1063,84],{"class":83},[61,1065,1066],{"class":87},"Enter your score: ",[61,1068,84],{"class":83},[61,1070,93],{"class":79},[61,1072,1073,1075,1077,1079,1082,1084,1087,1090,1092,1094,1096],{"class":63,"line":96},[61,1074,99],{"class":75},[61,1076,80],{"class":79},[61,1078,84],{"class":83},[61,1080,1081],{"class":87},"Your score plus one is ",[61,1083,84],{"class":83},[61,1085,1086],{"class":71}," +",[61,1088,1089],{"class":79}," (",[61,1091,1055],{"class":114},[61,1093,468],{"class":71},[61,1095,472],{"class":471},[61,1097,447],{"class":79},[14,1099,1100],{},"Right:",[52,1102,1104],{"className":54,"code":1103,"language":56,"meta":57,"style":57},"score = int(input(\"Enter your score: \"))\nprint(\"Your score plus one is\", score + 1)\n",[18,1105,1106,1128],{"__ignoreMap":57},[61,1107,1108,1110,1112,1114,1116,1118,1120,1122,1124,1126],{"class":63,"line":64},[61,1109,1055],{"class":67},[61,1111,72],{"class":71},[61,1113,430],{"class":429},[61,1115,80],{"class":79},[61,1117,435],{"class":75},[61,1119,80],{"class":79},[61,1121,84],{"class":83},[61,1123,1066],{"class":87},[61,1125,84],{"class":83},[61,1127,447],{"class":79},[61,1129,1130,1132,1134,1136,1139,1141,1143,1146,1148,1150],{"class":63,"line":96},[61,1131,99],{"class":75},[61,1133,80],{"class":79},[61,1135,84],{"class":83},[61,1137,1138],{"class":87},"Your score plus one is",[61,1140,84],{"class":83},[61,1142,111],{"class":79},[61,1144,1145],{"class":114}," score ",[61,1147,468],{"class":71},[61,1149,472],{"class":471},[61,1151,93],{"class":79},[409,1153,1155,1156,1158],{"id":1154},"using-input-without-saving-the-result","Using ",[18,1157,20],{}," without saving the result",[14,1160,1161],{},"If you do not store the result, you cannot use it later.",[14,1163,1164],{},"Less useful:",[52,1166,1168],{"className":54,"code":1167,"language":56,"meta":57,"style":57},"input(\"Enter your name: \")\nprint(\"Hello\")\n",[18,1169,1170,1184],{"__ignoreMap":57},[61,1171,1172,1174,1176,1178,1180,1182],{"class":63,"line":64},[61,1173,435],{"class":75},[61,1175,80],{"class":79},[61,1177,84],{"class":83},[61,1179,88],{"class":87},[61,1181,84],{"class":83},[61,1183,93],{"class":79},[61,1185,1186,1188,1190,1192,1195,1197],{"class":63,"line":96},[61,1187,99],{"class":75},[61,1189,80],{"class":79},[61,1191,84],{"class":83},[61,1193,1194],{"class":87},"Hello",[61,1196,84],{"class":83},[61,1198,93],{"class":79},[14,1200,1201],{},"Better:",[52,1203,1204],{"className":54,"code":55,"language":56,"meta":57,"style":57},[18,1205,1206,1224],{"__ignoreMap":57},[61,1207,1208,1210,1212,1214,1216,1218,1220,1222],{"class":63,"line":64},[61,1209,68],{"class":67},[61,1211,72],{"class":71},[61,1213,76],{"class":75},[61,1215,80],{"class":79},[61,1217,84],{"class":83},[61,1219,88],{"class":87},[61,1221,84],{"class":83},[61,1223,93],{"class":79},[61,1225,1226,1228,1230,1232,1234,1236,1238,1240],{"class":63,"line":96},[61,1227,99],{"class":75},[61,1229,80],{"class":79},[61,1231,84],{"class":83},[61,1233,106],{"class":87},[61,1235,84],{"class":83},[61,1237,111],{"class":79},[61,1239,115],{"class":114},[61,1241,93],{"class":79},[409,1243,1245],{"id":1244},"writing-a-prompt-that-is-unclear","Writing a prompt that is unclear",[14,1247,1248],{},"A good prompt tells the user exactly what to enter.",[14,1250,1251],{},"Unclear:",[52,1253,1255],{"className":54,"code":1254,"language":56,"meta":57,"style":57},"value = input(\"Enter value: \")\n",[18,1256,1257],{"__ignoreMap":57},[61,1258,1259,1262,1264,1266,1268,1270,1273,1275],{"class":63,"line":64},[61,1260,1261],{"class":67},"value ",[61,1263,72],{"class":71},[61,1265,76],{"class":75},[61,1267,80],{"class":79},[61,1269,84],{"class":83},[61,1271,1272],{"class":87},"Enter value: ",[61,1274,84],{"class":83},[61,1276,93],{"class":79},[14,1278,1279],{},"Clearer:",[52,1281,1283],{"className":54,"code":1282,"language":56,"meta":57,"style":57},"value = input(\"Enter your age in whole numbers: \")\n",[18,1284,1285],{"__ignoreMap":57},[61,1286,1287,1289,1291,1293,1295,1297,1300,1302],{"class":63,"line":64},[61,1288,1261],{"class":67},[61,1290,72],{"class":71},[61,1292,76],{"class":75},[61,1294,80],{"class":79},[61,1296,84],{"class":83},[61,1298,1299],{"class":87},"Enter your age in whole numbers: ",[61,1301,84],{"class":83},[61,1303,93],{"class":79},[409,1305,1307],{"id":1306},"leading-or-trailing-spaces-in-input","Leading or trailing spaces in input",[14,1309,1310],{},"Sometimes the user accidentally types extra spaces.",[14,1312,1313],{},"You can check this with:",[52,1315,1317],{"className":54,"code":1316,"language":56,"meta":57,"style":57},"user_input = input(\"Enter something: \")\n\nprint(user_input)\nprint(type(user_input))\nprint(repr(user_input))\n",[18,1318,1319,1339,1343,1354,1369],{"__ignoreMap":57},[61,1320,1321,1324,1326,1328,1330,1332,1335,1337],{"class":63,"line":64},[61,1322,1323],{"class":67},"user_input ",[61,1325,72],{"class":71},[61,1327,76],{"class":75},[61,1329,80],{"class":79},[61,1331,84],{"class":83},[61,1333,1334],{"class":87},"Enter something: ",[61,1336,84],{"class":83},[61,1338,93],{"class":79},[61,1340,1341],{"class":63,"line":96},[61,1342,712],{"emptyLinePlaceholder":711},[61,1344,1345,1347,1349,1352],{"class":63,"line":708},[61,1346,99],{"class":75},[61,1348,80],{"class":79},[61,1350,1351],{"class":114},"user_input",[61,1353,93],{"class":79},[61,1355,1356,1358,1360,1363,1365,1367],{"class":63,"line":715},[61,1357,99],{"class":75},[61,1359,80],{"class":79},[61,1361,1362],{"class":429},"type",[61,1364,80],{"class":79},[61,1366,1351],{"class":114},[61,1368,447],{"class":79},[61,1370,1371,1373,1375,1378,1380,1382],{"class":63,"line":734},[61,1372,99],{"class":75},[61,1374,80],{"class":79},[61,1376,1377],{"class":75},"repr",[61,1379,80],{"class":79},[61,1381,1351],{"class":114},[61,1383,447],{"class":79},[14,1385,1386],{},"These lines help you debug:",[26,1388,1389,1395,1401],{},[29,1390,1391,1394],{},[18,1392,1393],{},"print(user_input)"," shows the visible value",[29,1396,1397,1400],{},[18,1398,1399],{},"print(type(user_input))"," shows the data type",[29,1402,1403,1406,1407],{},[18,1404,1405],{},"print(repr(user_input))"," shows hidden spaces like ",[18,1408,1409],{},"' hello '",[14,1411,1412,1413,1416],{},"If needed, you can remove extra spaces with ",[18,1414,1415],{},".strip()",":",[52,1418,1420],{"className":54,"code":1419,"language":56,"meta":57,"style":57},"name = input(\"Enter your name: \").strip()\nprint(\"Hello,\", name)\n",[18,1421,1422,1447],{"__ignoreMap":57},[61,1423,1424,1426,1428,1430,1432,1434,1436,1438,1441,1444],{"class":63,"line":64},[61,1425,68],{"class":67},[61,1427,72],{"class":71},[61,1429,76],{"class":75},[61,1431,80],{"class":79},[61,1433,84],{"class":83},[61,1435,88],{"class":87},[61,1437,84],{"class":83},[61,1439,1440],{"class":79},").",[61,1442,1443],{"class":114},"strip",[61,1445,1446],{"class":79},"()\n",[61,1448,1449,1451,1453,1455,1457,1459,1461,1463],{"class":63,"line":96},[61,1450,99],{"class":75},[61,1452,80],{"class":79},[61,1454,84],{"class":83},[61,1456,106],{"class":87},[61,1458,84],{"class":83},[61,1460,111],{"class":79},[61,1462,115],{"class":114},[61,1464,93],{"class":79},[47,1466,1468],{"id":1467},"faq","FAQ",[409,1470,1472,1473,1475],{"id":1471},"what-does-input-return-in-python","What does ",[18,1474,20],{}," return in Python?",[14,1477,1478],{},"It returns a string, even if the user types digits.",[409,1480,1482],{"id":1481},"how-do-i-get-a-number-from-user-input","How do I get a number from user input?",[14,1484,1485,1486,1488,1489,1491,1492,1494],{},"Wrap ",[18,1487,20],{}," with ",[18,1490,129],{}," for whole numbers or ",[18,1493,136],{}," for decimals.",[409,1496,1498],{"id":1497},"why-does-my-program-crash-when-i-type-letters-instead-of-a-number","Why does my program crash when I type letters instead of a number?",[14,1500,1501,804,1503,1505,1506,1508],{},[18,1502,129],{},[18,1504,136],{}," raise ",[18,1507,591],{}," if the text cannot be converted.",[409,1510,1512],{"id":1511},"can-i-ask-for-input-more-than-once","Can I ask for input more than once?",[14,1514,1515,1516,1518],{},"Yes. Call ",[18,1517,20],{}," again each time you need another value.",[47,1520,1522],{"id":1521},"see-also","See also",[26,1524,1525,1531,1536,1542,1548,1557],{},[29,1526,1527],{},[124,1528,255,1529,258],{"href":254},[18,1530,20],{},[29,1532,1533],{},[124,1534,1535],{"href":580},"How to convert user input to numbers in Python",[29,1537,1538],{},[124,1539,255,1540,258],{"href":126},[18,1541,129],{},[29,1543,1544],{},[124,1545,255,1546,258],{"href":133},[18,1547,136],{},[29,1549,1550],{},[124,1551,1552,1553,804,1555,934],{"href":928},"How to use ",[18,1554,803],{},[18,1556,807],{},[29,1558,1559],{},[124,1560,1561,1562],{"href":647},"Fix ",[18,1563,651],{},[14,1565,1566],{},"Next step: learn how to convert input safely and handle bad input without crashing.",[1568,1569,1570],"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 .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 .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":57,"searchDepth":96,"depth":96,"links":1572},[1573,1574,1575,1577,1578,1585,1586,1587,1596,1603],{"id":49,"depth":96,"text":50},{"id":139,"depth":96,"text":140},{"id":160,"depth":96,"text":1576},"Basic syntax of input()",{"id":261,"depth":96,"text":262},{"id":393,"depth":96,"text":394,"children":1579},[1580,1582,1584],{"id":411,"depth":708,"text":1581},"Use int() for whole numbers",{"id":485,"depth":708,"text":1583},"Use float() for decimal numbers",{"id":584,"depth":708,"text":585},{"id":654,"depth":96,"text":655},{"id":788,"depth":96,"text":789},{"id":937,"depth":96,"text":938,"children":1588},[1589,1591,1592,1594,1595],{"id":941,"depth":708,"text":1590},"Forgetting that input() returns a string",{"id":1038,"depth":708,"text":1039},{"id":1154,"depth":708,"text":1593},"Using input() without saving the result",{"id":1244,"depth":708,"text":1245},{"id":1306,"depth":708,"text":1307},{"id":1467,"depth":96,"text":1468,"children":1597},[1598,1600,1601,1602],{"id":1471,"depth":708,"text":1599},"What does input() return in Python?",{"id":1481,"depth":708,"text":1482},{"id":1497,"depth":708,"text":1498},{"id":1511,"depth":708,"text":1512},{"id":1521,"depth":96,"text":1522},"Master how to get user input in python in our comprehensive Python beginner guide.","md",{},"\u002Fhow-to\u002Fhow-to-get-user-input-in-python",{"title":5,"description":1604},"how-to\u002Fhow-to-get-user-input-in-python","9b_bD7u552cRge0NIBuX-7-J8tTEAbqIXXeh_emQ68I",1777585507547]