Friday, July 16, 2021

# 5. Python Input function

                                             Input ( ) function in python

Now let us make computer to take input from us.

Lets start our journey with python!


Python has an input function which lets you ask a user for some text input. You call this function to tell the program to stop and wait for the user to key in the data.

Taking user input by using input() function.

>>> input ( ) 

I am learning python

Output : ‘ I am learning python ‘

Now lets make it more meaningful to take input from the user. Add some meaningful text inside function using either single ' ' or double quotes " ".

input() function by default will take everything as string.

>>> input ( “ Enter your name: ” )

Enter your name: Satish

Output : ‘ Satish ‘

>>> name = input ( “ enter your name : “)

        age = input ( “ enter your age : “ )

        print ( name, age )

enter your name : Satish

enter your age : 35

Output :  Satish    35

In the above program both name and age are in string format.

>>>  name = input ( “ enter your name : “)

        age = input ( “ enter your age : “ )

        print ( name, age )

enter your name : Satish

enter your age : python

Output : Satish        python

“ “ “ In the above program whenever we enter age as string it is accepting because input() function is by default string. But age should not be a string it should be a number, to get this we should convert string to integer.” “ “

int values:

>>>name = input ( “ enter your name : “)

        age =int ( input ( “ enter your age : “ ) )

        print ( name, age )

enter your name : Satish

enter your age : 35

Output :  Satish    35

#  In the above program if we try to enter string in age we will get error

float values:

>>> name = input ( “ enter your name : “)

        age =int( input ( “ enter your age : “ ) )

        weight = float (input (“ enter your weight: ” ) )

        print ( name, age, weight )

enter your name : Satish

enter your age : 35

enter your weight : 55.5

Output :  Satish    35      55.5

Practice Programs:

1.    Create a login form.

2.    Using input() function ask for user's name.

3.    Ask user a numerical question, such as, "Please enter your age."

4.    Using input() function, print() function and ask for the current year, then print the answer  

5. Accept two numbers from the user and perform multiplication,addition, subtraction, division, modulus, floor division, power 

 

    project: 

     In a college, an awareness webinar was conducted on covid-19 topic. That was conducted by the health department. After the program the department want to know how was the program as feedback from the students. But its not hygienic to take feedback manually, so the department decided to take feedback digitally. Can you help the department by writing program on feedback form so that they can easily get their feedback.


Refer next post for answers

Thanks for visiting

 

 

 

 

No comments:

Post a Comment

# 7. Type conversion in Python

                                      Type Conversion in Python Python defines type conversion functions to directly convert one data type t...