Taking user input
It's very similar to the print command, except that it'll show the message in the console then wait until the user has typed something into the console and pressed enter.
Let's get your hands dirty with the below code :-
What is a variable?input asks for something, takes it, but then has nowhere to put it. We can change that with a variable which is a value that we can use to name and store data. (Remember, David's box for David's phone?)
Naming variables
myName = input("What is your name ?: ")
myFood = input("What is your food ?: ")
myMusic= input("what is your fav music?:")
myLocation = input("where do u live?:")
print("You are")
print(myName)
print("You are probably hungry for")
print(myFood)
print("You must heard of ")
print(myMusic)
print("Living in ")
print(myLocation)
output /Example
What is your name ?: Annat
What is your food ?: mango
what is your fav music?: Trans
where do u live?: Singapore
You are
Annat
You are probably hungry for
mango
You must heard of
Trans
Living in
Singapore
PS- bold words are input words given by the user.
Here in this above code, when we are taking an input as "what is your name? : " with the input function then automatically it is looking for a name in the console and when you have give the name it will be stored in the "myName" variable. when we call the "myName" variable with another print function then it will print the answer of input function that is given by you.
Naming variables
You can give a variable any name you want, but you can't use spaces.
You can use:
- underscores_between_words
- camelCaseToMakeItEasierToRead
Printing a Variable
You can print your variable using print and the name you used for your variable in your input command. Remember the three variables we just created: myName , myFood, myMusic
In your code you can now print 'name' by using print(myName) or 'Food' by using print(myFood).
No comments:
Post a Comment