--Originally published at Quirino´s Projects
When creating console programs, in python or any other programming language, we have a few options when it comes to collecting data from the user, a really helpful one is input().
input is a function that prints to console the argument given to it, then enables user input and returns it as a string
Useful examples
name = input(“What is your name “)
#”Whats your name gets printed to the console”
#input() Returns the string that the user gave
The example above gets stored as a string, this is useful when we want to work with them, but becomes problematic when we want to work with numbers, in that case we can use int()
num = int(input(“Give me a number “))
#The string is converted to an integer
NOTE: We should always expect that the user will try to mess with our program, check my post about Validating user input for this
Using the input() function can be used for checking what the user wants to do inside our program
def converter(userInput):
if userInput() == “C”:
convertToC(input(“Give me the temperature”))
if uInput.upper() == “F”:
convertToC(input(“Give me the temperature”))
else:
print(“Invalid input”)
converter(userInput)
Also input() can be very practical to create other programs, check my TicTacToe.py in my GitHub https://github.com/QuirinoC/TicTacToe.py