Addition operation by user input value in python [duplicate]
This question already has an answer here:
How can I read inputs as numbers?
17 answers
I'm newbie for the Python. Here I'm trying to add two value by using
x = 10
y= 20
print("Addition value is ", x+y )
It's return the proper result as: Addition value is 30.
But while I'm read the input by the user by using input() . It's just concatenate the x and y value. For ex)
x = input('Enter first number:')
y = input('Enter the second number')
z = x + y
print("The addition value is:",z)
Imagine if I give user input for first number as 10 second number as 20 means
It's return the result as "The addition value is: 1020
I'm not sure why it's concatenate the two values instead of adding two values
Is I did any mistake on my code. Please correct me. Thanks in advance.
python python-3.x
marked as duplicate by Chris_Rands
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 14 '18 at 13:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How can I read inputs as numbers?
17 answers
I'm newbie for the Python. Here I'm trying to add two value by using
x = 10
y= 20
print("Addition value is ", x+y )
It's return the proper result as: Addition value is 30.
But while I'm read the input by the user by using input() . It's just concatenate the x and y value. For ex)
x = input('Enter first number:')
y = input('Enter the second number')
z = x + y
print("The addition value is:",z)
Imagine if I give user input for first number as 10 second number as 20 means
It's return the result as "The addition value is: 1020
I'm not sure why it's concatenate the two values instead of adding two values
Is I did any mistake on my code. Please correct me. Thanks in advance.
python python-3.x
marked as duplicate by Chris_Rands
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 14 '18 at 13:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How can I read inputs as numbers?
17 answers
I'm newbie for the Python. Here I'm trying to add two value by using
x = 10
y= 20
print("Addition value is ", x+y )
It's return the proper result as: Addition value is 30.
But while I'm read the input by the user by using input() . It's just concatenate the x and y value. For ex)
x = input('Enter first number:')
y = input('Enter the second number')
z = x + y
print("The addition value is:",z)
Imagine if I give user input for first number as 10 second number as 20 means
It's return the result as "The addition value is: 1020
I'm not sure why it's concatenate the two values instead of adding two values
Is I did any mistake on my code. Please correct me. Thanks in advance.
python python-3.x
This question already has an answer here:
How can I read inputs as numbers?
17 answers
I'm newbie for the Python. Here I'm trying to add two value by using
x = 10
y= 20
print("Addition value is ", x+y )
It's return the proper result as: Addition value is 30.
But while I'm read the input by the user by using input() . It's just concatenate the x and y value. For ex)
x = input('Enter first number:')
y = input('Enter the second number')
z = x + y
print("The addition value is:",z)
Imagine if I give user input for first number as 10 second number as 20 means
It's return the result as "The addition value is: 1020
I'm not sure why it's concatenate the two values instead of adding two values
Is I did any mistake on my code. Please correct me. Thanks in advance.
This question already has an answer here:
How can I read inputs as numbers?
17 answers
python python-3.x
python python-3.x
edited Nov 14 '18 at 13:18
AkshayNevrekar
4,92791840
4,92791840
asked Nov 14 '18 at 13:12
Kannan.PKannan.P
20716
20716
marked as duplicate by Chris_Rands
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 14 '18 at 13:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Chris_Rands
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 14 '18 at 13:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Just change your code to:
x = int(input('Enter first number:'))
y = int(input('Enter the second number'))
z = x + y
print("The addition value is:",z)
Reasoning:
The type of user input(input method) is always string.
If you perform + on strings it will concatenate both values instead of adding.
So first convert them into int(or float)
Thanks for your quick attention on these May I know why we need to specify as int() here ?
– Kannan.P
Nov 14 '18 at 13:15
See the reasoning part. I hope it is clear.
– AkshayNevrekar
Nov 14 '18 at 13:16
Yes. Thank you. I will take look on these
– Kannan.P
Nov 14 '18 at 13:17
@Kannan.P Read the duplicate,input()always returns a string in Python 3, which you are clearly using- next time don't tag your question with both python versions
– Chris_Rands
Nov 14 '18 at 13:17
1
Sure @Chris_Rands. I will keep in mind for my next level of questions :)
– Kannan.P
Nov 14 '18 at 13:25
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Just change your code to:
x = int(input('Enter first number:'))
y = int(input('Enter the second number'))
z = x + y
print("The addition value is:",z)
Reasoning:
The type of user input(input method) is always string.
If you perform + on strings it will concatenate both values instead of adding.
So first convert them into int(or float)
Thanks for your quick attention on these May I know why we need to specify as int() here ?
– Kannan.P
Nov 14 '18 at 13:15
See the reasoning part. I hope it is clear.
– AkshayNevrekar
Nov 14 '18 at 13:16
Yes. Thank you. I will take look on these
– Kannan.P
Nov 14 '18 at 13:17
@Kannan.P Read the duplicate,input()always returns a string in Python 3, which you are clearly using- next time don't tag your question with both python versions
– Chris_Rands
Nov 14 '18 at 13:17
1
Sure @Chris_Rands. I will keep in mind for my next level of questions :)
– Kannan.P
Nov 14 '18 at 13:25
add a comment |
Just change your code to:
x = int(input('Enter first number:'))
y = int(input('Enter the second number'))
z = x + y
print("The addition value is:",z)
Reasoning:
The type of user input(input method) is always string.
If you perform + on strings it will concatenate both values instead of adding.
So first convert them into int(or float)
Thanks for your quick attention on these May I know why we need to specify as int() here ?
– Kannan.P
Nov 14 '18 at 13:15
See the reasoning part. I hope it is clear.
– AkshayNevrekar
Nov 14 '18 at 13:16
Yes. Thank you. I will take look on these
– Kannan.P
Nov 14 '18 at 13:17
@Kannan.P Read the duplicate,input()always returns a string in Python 3, which you are clearly using- next time don't tag your question with both python versions
– Chris_Rands
Nov 14 '18 at 13:17
1
Sure @Chris_Rands. I will keep in mind for my next level of questions :)
– Kannan.P
Nov 14 '18 at 13:25
add a comment |
Just change your code to:
x = int(input('Enter first number:'))
y = int(input('Enter the second number'))
z = x + y
print("The addition value is:",z)
Reasoning:
The type of user input(input method) is always string.
If you perform + on strings it will concatenate both values instead of adding.
So first convert them into int(or float)
Just change your code to:
x = int(input('Enter first number:'))
y = int(input('Enter the second number'))
z = x + y
print("The addition value is:",z)
Reasoning:
The type of user input(input method) is always string.
If you perform + on strings it will concatenate both values instead of adding.
So first convert them into int(or float)
edited Nov 14 '18 at 13:17
answered Nov 14 '18 at 13:13
AkshayNevrekarAkshayNevrekar
4,92791840
4,92791840
Thanks for your quick attention on these May I know why we need to specify as int() here ?
– Kannan.P
Nov 14 '18 at 13:15
See the reasoning part. I hope it is clear.
– AkshayNevrekar
Nov 14 '18 at 13:16
Yes. Thank you. I will take look on these
– Kannan.P
Nov 14 '18 at 13:17
@Kannan.P Read the duplicate,input()always returns a string in Python 3, which you are clearly using- next time don't tag your question with both python versions
– Chris_Rands
Nov 14 '18 at 13:17
1
Sure @Chris_Rands. I will keep in mind for my next level of questions :)
– Kannan.P
Nov 14 '18 at 13:25
add a comment |
Thanks for your quick attention on these May I know why we need to specify as int() here ?
– Kannan.P
Nov 14 '18 at 13:15
See the reasoning part. I hope it is clear.
– AkshayNevrekar
Nov 14 '18 at 13:16
Yes. Thank you. I will take look on these
– Kannan.P
Nov 14 '18 at 13:17
@Kannan.P Read the duplicate,input()always returns a string in Python 3, which you are clearly using- next time don't tag your question with both python versions
– Chris_Rands
Nov 14 '18 at 13:17
1
Sure @Chris_Rands. I will keep in mind for my next level of questions :)
– Kannan.P
Nov 14 '18 at 13:25
Thanks for your quick attention on these May I know why we need to specify as int() here ?
– Kannan.P
Nov 14 '18 at 13:15
Thanks for your quick attention on these May I know why we need to specify as int() here ?
– Kannan.P
Nov 14 '18 at 13:15
See the reasoning part. I hope it is clear.
– AkshayNevrekar
Nov 14 '18 at 13:16
See the reasoning part. I hope it is clear.
– AkshayNevrekar
Nov 14 '18 at 13:16
Yes. Thank you. I will take look on these
– Kannan.P
Nov 14 '18 at 13:17
Yes. Thank you. I will take look on these
– Kannan.P
Nov 14 '18 at 13:17
@Kannan.P Read the duplicate,
input() always returns a string in Python 3, which you are clearly using- next time don't tag your question with both python versions– Chris_Rands
Nov 14 '18 at 13:17
@Kannan.P Read the duplicate,
input() always returns a string in Python 3, which you are clearly using- next time don't tag your question with both python versions– Chris_Rands
Nov 14 '18 at 13:17
1
1
Sure @Chris_Rands. I will keep in mind for my next level of questions :)
– Kannan.P
Nov 14 '18 at 13:25
Sure @Chris_Rands. I will keep in mind for my next level of questions :)
– Kannan.P
Nov 14 '18 at 13:25
add a comment |