Table of contents
Introduction
In this blog, I will talk about, how we can take input from the user using the input() function so that there is no need for manual intervention. I also wrote a bunch of code to practice the input() function
So let's get started...
Input()
In python, to get the user input we make use of the input() function. This function returns a value as a string/character that we can pass into the variable and print as a message on the console.
Syntax
variable = input()
The input function above returns the value as a " string ". Hence we need to TYPECAST it into another datatype whenever required.
variable = input()
x = int(variable)
#or
variable = int(input())
variable = float(input())
Using the input() function we can also display text. This input() function takes user input and displays it as a message
Example
a = input("Enter your name: ")
print(a)
Enter your name: Chintan
Chintan
Program
a = input("Enter your name: ")
print("Hi, ",a, "I am computer. How are you ? \n")
print("Lets add two numbers taking input from users\n ")
x = input("Enter first number: ")
y = input("Enter second number: ")
#whenever user gives input python takes it by default as string
#In the below statement value of x and y are in string format so we need to typecast them into 'integer' and convert them into numbers for addition processing
print("Addition of", x ,"and", y, "is", int(x) + int(y))
print(int(x) + int(y))
#output
Enter your name: abc
Hi, abc I am computer. How are you ?
Lets add two numbers taking input from users
Enter first number: 1
Enter second number: 2
Addition of 1 and 2 is 3
Resources Used
You can watch the video of Day#10 by clicking on the below link ๐๐๐๐๐
Conclusion
Thanks, guys for going through this blog post. With this blog post, today I completed 10 days of python coding and blogging on HASHNODE. With today's learning, and accepting user input we are now scaling up into the actual python world. I hope if you are a newbie you got to learn something.
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practising coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.