How to Input Numbers in Python
Programmers often use the Python programming language to write small or simple codes that perform a singular tasks. Python lends itself to this type of use because of its easy syntax and ready-to-use functions. For example, if you wanted to build a small script that takes input from the user in an integer, or whole number, format, it is as simple as using the "raw_input" function and a quick conversion.
Instructions
-
-
1
Declare a variable that will hold the user input.
>>>x = 0
-
2
Use the "raw_input" function to gather data from the user. The function takes a string argument, which will present the user with a prompt (Source 1):
>>>x = raw_input('Input a Number: ')
Input a Number: 5
>>>x
'5' //string 5 -
-
3
When the user enters input, Python accepts is as a string of characters. To change it to an integer, use the "int" function (Source 1):
>>>x = int(raw_input('Input a Number: '))
Input a Number: 5
>>>x
5 //integer 5
-
1