By the end of this lesson you should be able to:
1.Describe the input command and its basic usage in python.
2.Code using the input command.
3.Avoid the common errors associated with the input command.
The input command
-The input command as the name suggests is used when the user is expected to key in information into the computer.
-A message is shown on the console and the user is expected to type information then press enter for the rest of the code to run.
-The input function has the following basic structure: input("text")
.A point to note is that the text input should be written in small letters for the code to work.
-Here's an example that demonstrates the basic structure of input function()
input("What's your name?")
-If you try running the above code in your python code editor you should be able to key in your name.
-But how do we make use of the user's input? Next up variables
Variables
-Variables are used with the input function to store the information that the user keyed in.
-Here's a basic structure of an input function with a variable: variable = input("")
Example in code:
myname = input("What's your name?")
-Once the user inputs their name after the question and presses enter, the name which the user keyed in is 'stored' in the variable myname as a text(string).
Naming variables
-You can name your variable in whichever way you would like but it is advisable to use a name that is related to the information that you expect to be keyed in. For example
age = input("How old are you?")
-Do not use spaces when naming variables, instead:
•use_underscores_between_words
•camelCaseMethodToMakeItEasierToRead
using underscores
favorite_hobby = input("What is your most favorite hobby?")
camel case method
favoriteHobby = input("Which hobby do you enjoy the most?")
Printing variables
-We have already learnt the art of storing the user's input in a variable. Your next question should be; How do we get the information stored in the variable to be displayed on the console?
-We simply print the variable by using the print statement that we learnt in the previous lesson.
myname = input("What is your name?:")
print("So you are")
print(myname)
-Try running the code above.You will notice that the name that the you typed after the question will be printed in the last line.
-When printing a variable, do not use the quotation marks "".
-Here's another example:
nationality = input("What is your nationality?:")
age = input("How old are you?:")
hobby = input("What do you love doing in your free time?:")
print()
print("So you are a/an")
print(nationality)
print("aged")
print(age)
print("and your hobby is")
print(hobby)
Common errors associated with the input function
Error 1- Capitalization
color = Input("What's your favorite color?:")
-The code will not work because the 'i' in input has been capitalised.
-Therefore the text input must be written in small letters for the code to function.
-The correct format should be:
color = input("What's your favorite color?:")
Error 2- Wrong syntax
my name = input("What's your name?:")
-The error above is that there's a space in the variable myname.
-The correct format should be:
myname = input("What's your name?:")
Error 3- ....
myname = input("What is your name?:")
print("So you are")
print("myname")
-In this case, the text that will be printed in the last line will be myname instead of the user's name e.g Samuel.
-to fix this remove the quotation marks.
myname = input("What is your name?:")
print("So you are")
print(myname)
Error 4- Name error
favoriteShow = input("What's your favorite show?:")
print("Your favorite show is")
print(favoriteshow)
-In the code above the printed variable favouriteshow does not match does not match the variable in the first line, the favoriteshow in line 3 is therefore undefined.
-The correct format should therefore be:
#the variable name should remain the same otherwise it is classified as undefined.
favoriteShow = input("What's your favorite show?:")
print("Your favorite show is")
print(favoriteShow)
-Please leave any concerns or questions in the comments section below.
That's all for today.
Next up;Concatenation!
Top comments (0)