If you need to interact with the user when running your program in a command line, to ask for a piece of information, you can use the input()
function in Python 3.
country = input("What is your country? ") #user enters 'Brazil'
print(country)
#output: Brazil
The captured value is always string
, you need to convert it using typecasting if needed.
age = input("How old are you? ") #user enters '29'
print(age)
#output: 29
print(type(age))
#<class 'str'>
age = int(age)
print(type(age))
#<class 'int'>
Notice the age 29 is captured as string
and then converted explicitly to int
.
You can learn more about types in this post about Python Data Types.