Python Exercise Programs #001 – Create a Python Program To Take User Input

create-a-python-program-that-take-user-input

Hey Guy’s, Welcome to PracticeHouse. The house is full of exercises program to increase our skill level up.

In this post I am going to share our first exercise program for python is – Create a program that takes the name and the age from the user as input and then send them a message that which year they will be 100 years old.

pretty cool !!! isn’t it. Let’s see our code for the program to take user input and calculate their age.

okay let’s see the code first if you are a beginner then don’t worry down there i will explain the code also and if you are familiar with python code then you good to go.

Sample Code:

Here is the sample output :

Explanation :

Okay folks, Let me explain this program if you didn’t understand. it’s a very simple program in python.

first of all, you can see that i saved this in a file called exercise1.py So .py is the file extension for the python script file.

To run a python script in a shell or CLI you have to open the CLI into that respective folder or you can navigate through CLI into that folder then use python and then your filename.

Then we use input() function to take user input. So by default, it returns us String.

To Convert the string into integer we used int() function for our age variable.

To get the current date and time we import a library called datetime by using import datetime. then we use datetime.datetime.now() function to get the current date and store into a variable called today. then use today.year to get the current year.

then we just did a small calculate when the user will be 100 years old.

Then in python, we use print() function to printout something to our console.

That’s it.

So if you have any other way to show us. you are most welcome to share your code down there.

 
Python Home 

 

Leave a Reply

Your email address will not be published. Required fields are marked *