2

In the below code, when the inputs to a,b and c are 2,3 and 4 respectively,

a=input('Enter length of first side of triangle: ')
b=input('Enter length of second side of triangle: ')
c=input('Enter length of third side of triangle: ')
print((a+b)>c)

The output is

False

But if the inputs are changed to float (as depicted below),

a=float(input('Enter length of first side of triangle: '))
b=float(input('Enter length of second side of triangle: '))
c=float(input('Enter length of third side of triangle: '))
print((a+b)>c)

then the output is

True

Please explain why this is happening

  • 3
    You are comparing `str` in your first snippet. – Chris Mar 07 '19 at 07:01
  • 1
    Convert to `int` and then compare. – DirtyBit Mar 07 '19 at 07:01
  • 2
    `input` returns a string representing the sequence of characters entered, not an int representing the numeric value of the input parsed as a base-10 integer, even if the input happens to be parseable as such. String `+` and `>` don't work by numeric value. – user2357112 Mar 07 '19 at 07:03
  • 1
    Possible duplicate of [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – Antwane Mar 07 '19 at 07:07
  • 1
    @SrinivasGadepalli Consider accpeting an answer that helped you understand: https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – DirtyBit Mar 07 '19 at 07:14

5 Answers5

4

Result of your first snippet is:

('2' + '3') > '4'
# which is equivalent to:
'23' > '4'

In python, strings are compared based on their unicode value, one character at the time. Thus the above comparison becomes:

ord('2') > ord('4')
# which is equivalent to
50 > 52

which is False.

On the other hand, your second snippet is a simple float comparison:

(2.0 + 3.0) > 4.0

which is True

Chris
  • 29,127
  • 3
  • 28
  • 51
2

Previously:

a=input('Enter length of first side of triangle: ')   #  '2'
b=input('Enter length of second side of triangle: ')  #  '3'
c=input('Enter length of third side of triangle: ')   #  '4' 
print((a+b)>c)   # ('2' + '3') > '4' becoming '23' > '4' 

input returns a str, Convert it to int and then compare:

a=int(input('Enter length of first side of triangle: '))    # 2
b=int(input('Enter length of second side of triangle: '))   # 3
c=int(input('Enter length of third side of triangle: '))    # 4
print((a+b)>c)    # (2 + 3) > 4   becoming 5 > 4 
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
1

How about converting the input which is string to int

a=int(input('Enter length of first side of triangle: '))
b=int(input('Enter length of second side of triangle: '))
c=int(input('Enter length of third side of triangle: '))
print((a+b)>c)

This gives

Enter length of first side of triangle: 2
Enter length of second side of triangle: 3
Enter length of third side of triangle: 4
True
Anurag A S
  • 725
  • 10
  • 23
1

The input function returns the string value & you are comparing the string values not the integers.

Please type-cast the input values to integer by following piece of code .

a=int(input('Enter length of first side of triangle: '))
b=int(input('Enter length of second side of triangle: '))
c=int(input('Enter length of third side of triangle: '))
print((a+b)>c)

or

a=input('Enter length of first side of triangle: ')
b=input('Enter length of second side of triangle: ')
c=input('Enter length of third side of triangle: ')
print((int(a)+int(b))>int(c))
Usman
  • 1,983
  • 15
  • 28
0

The return type of input() is string. So when you are doing ('1'+'2') as string the output is 12.

So you would need to convert the string to numeric datatype using int() or float() and that would work.

Keval Dave
  • 2,777
  • 1
  • 13
  • 16