Asking Question – Python

Asking Question

Now it is time to pick up the pace and start asking question to user. You are doing a lot of printing to get you familiar with typing simple things, but those simple things are fairly boring. What we want to do now is get data into your programs. This is a little tricky because you have to learn to do two things that may not make sense right away but trust me and do it anyway. It will make sense in a few exercises.

Most of what software does is the following:

  1. Take some kind of input from a person.
  2. Change it.
  3. Print out something to show how it changed.

So far you have been printing strings, but you haven’t been able to get any input from a person. You may not even know what “input” means, but type this code in anyway and make it exactly the same. In the next exercise we’ll do more to explain input. Let’s see a code (Just copy, past and run).

print ("Hello Now I want some input from you")
Name = input("What is your name? ")
age = input("What is your age? ")
print ("Hello %s your age is %s" % (Name,age))

Let’s see the output and understand the functions we used.
Taking Input in Python
Here you see your function is asking for Input, enter your name and then enter your age.
How to Take input in python

Input Function

Input function is used to take input and assign it to another variable. You can also pass some instruction string to user like question to take input. If you don’t want to pass any instruction string then you can simply leave it empty. You can use print function to provide instruction. Check another script
print ("Hello What is your Name")
Name = input()
print ("Hello %s Please tell me your age" % (Name))
age = input()
print ("%s Can you update me your birth month" % (Name), end=" ")
mnth = input()
print ("Day", end = " ")
day = input()
print ("year", end=" ")
year = input()
print ("\n\n\nDetails entered by you are \nName %s\nAge %s\nBirth date(m-d-y) %s-%s-%s" % (Name, age, mnth,day,year))
Run it and see output
 asking question in python
We used end function, so that our program prompt to user after the question; just like passing instruction string.
Exercise of the day:-
Take some numeric input and apply mathematical algorithm on them.

no comments

Leave me comment