Learn Python - Bit by bit #3
<< Learn Python - Bit by bit #2 - Variable
input()
So far we have been running a few instructions that always produce the same result. Which is kinda boring. So today we are focusing on input function. input() takes input from the user. Pretty obvious duh!
Go to repl.it and choose Python3 as the language. Inside main.py type:
msg = input()
print(msg)
Now click run.
On the console type "Hello World!" and press enter. You will notice that the same message will be printed back.
Now it's not pretty obvious to the user who doesn't have access to the code what to do because when you click run it doesn't show anything. So in order to avoid this problem let's show the user what he or she needs to do. input() allows to take an optional string (text) as an argument (something that goes inside ()) and when the function is run it displays the string we passed to it.
msg = input("Please enter a message: ")
print(msg)
We can even do something like this:
msg = input("Please enter a message: ")
print("Message: ",msg)
What we just did is we provided two strings to the print() function separated by a comma so that it displays both strings in the same order we have provided.
So that's it for today folks!