If we want to perform calculations and need to get numbers from the user, the input function produces only strings, but we can use the int function to convert a properly formed string of digits into an integer.


Eg:

print("Enter the first value :")
a=input()
print("Enter the second value:")
b=input()
first=int(a)
second=int(b)
print("The Sum of the value is:",first+second)


OUTPUT:
Enter the first value :
10
Enter the second value:
20
The Sum of the value is: 30



The program halts after printing the first line and does not continue until the user provides the input, after the program prints the second message it again pauses to accept the user's second entry.

The input function optionally accepts a string that it prints just before the program stops to wait for the user to respond. (Eg: a=input("Enter your name:"))


Eg:

a = input("Enter the first value:")
b = input("Enter the second value:")
num1 = int(a)
num2 = int(b)
print("The Sum of the value is: ", num1 + num2)

OUTPUT:

Enter the first value:12
Enter the second value:43
The Sum of the value is:  55



We can combines the input and int functions into one statement.

Eg:

num1 = int(input("Enter the first value:"))
num2 = int(input("Enter the second value:"))
print("The sum of the value is:", num1 +num2)

OUTPUT:

Enter the first value:12
Enter the second value:32
The sum of the value is: 44


Post a Comment

Previous Post Next Post